组合wp_插入_POST()和UPDATE_POST_META()

时间:2013-10-17 作者:John Genry

如何组合wp_insert_post() 和wpupdate_post_meta?

我需要先插入我的帖子,然后更新帖子。让我知道一个例子?我想为我未来的项目提供一个样本。

1 个回复
最合适的回答,由SO网友:Oleg Butuzov 整理而成

2020 Update:

In case you landed here looking for a refresher on how to update the post\'s meta, as I did, see the following edit:

The post array argument in wp_insert_post() takes an inner array called meta_input, so you can create the post with proper meta in one database query.

$page_id = wp_insert_post(array(
    \'post_title\'    =>  \'Team\',
    \'menu_order\'    =>  \'2\',
    \'post_content\'  =>  \'[team]\',
    \'post_type\'     =>  \'page\',
    \'post_parent\'   =>  1,
    \'post_status\'   =>  \'publish\',
    \'meta_input\'    =>  array(
        \'your_meta_key\' => \'your meta value\'
    ),
));

The same can be done for wp_update_post():

wp_update_post(array(
  \'post_title\' => \'About\',
  \'ID\' => $page_id,
  \'meta_input\' =>  array(
    \'your_meta_key\' => \'your meta value\'
  ),
));

Original Answer:

is this code enough for you?

/*
** wp_insert_post - http://codex.wordpress.org/Function_Reference/wp_insert_post
** function will create a new post and return a $interger shich is a ID of the new created post.
*/
$page_id = wp_insert_post(array(
    \'post_title\'    =>  \'Team\',
    \'menu_order\'    =>  \'2\',
    \'post_content\'  =>  \'[team]\',
    \'post_type\'     =>  \'page\',
    \'post_parent\'   =>  1,
    \'post_status\'   =>  \'publish\',
));

/*
**  wp_update_post - http://codex.wordpress.org/Function_Reference/wp_update_post
** function is simular (actually its almost alias) of wp_insert_post
** it will update a post 
** $arguments that passed to wp_update_post is a same as in wp_insert_post but require additional argument ID whish is id of the post
*/
wp_update_post(array(\'post_title\' => \'About\', \'ID\' => $page_id));
结束

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在