WP insert post PHP function and Custom Fields

痴心易碎 提交于 2019-12-10 19:35:17

问题


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:


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



来源:https://stackoverflow.com/questions/4901544/wp-insert-post-php-function-and-custom-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!