WP插入PHP函数和自定义字段

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

dongqindu8110
dongqindu8110 在wordpress.stackexchange.com/questions/8569/...更新了问题和答案。
8 年多之前 回复

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

dongshilve4392
dongshilve4392 此答案的更新版本位于wordpress.stackexchange.com/questions/8569 / ...
8 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐