WP insert post PHP function and Custom Fields

2019-07-02 01:25发布

The wordpress function is used for submitting data programatically. Standard fields to submit to incude the content, excerpt, title, date and many more.

What there is no documentation for is how to submit to a custom field. I know it is possible with the add_post_meta($post_id, $meta_key, $meta_value, $unique); function.

What I don't know is how to include that into the standard wp_insert_post function. So the reason I ask you all is because this is more of a PHP question than a WP question. Below is the PHP code to submit the post.

<?php 
$my_post = array(
     'post_title' => $_SESSION['booking-form-title'],
     'post_date' => $_SESSION['cal_startdate'],
     'post_content' => 'This is my post.',
     'post_status' => 'publish',
     'post_type' => 'booking',
  );
  wp_insert_post( $my_post );
  ?>

Any help chaps,

Marvellous

1条回答
forever°为你锁心
2楼-- · 2019-07-02 02:28

If you look at the functions reference in the codex, you can see that wp_insert_post returns The ID of the post if the post is successfully added to the database.

Because of that, you can do so:

<?php 

$my_post = array(
    'post_title' => ...
);

$new_post_id = wp_insert_post( $my_post );

add_post_meta($new_post_id, $meta_key, $meta_value, $unique);

?>

Hope this helps

查看更多
登录 后发表回答