admin管理员组文章数量:1335618
I've JUST started using WordPress..i'm trying to add posts to Wordpress using php and i came across this piece of code:
// Create post object
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post );
i understand this is the way to go when creating a post using php..my questions are:
how do we execute the above php script and where do we save it in the WordPress folder?
what does the 'post_category' array mean(i'd like to use a category id to add posts to WordPress)?
i'd like to mention that i've done some descent searching on the net but the resources i've found do not mention how to execute the above script.
I've JUST started using WordPress..i'm trying to add posts to Wordpress using php and i came across this piece of code:
// Create post object
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post );
i understand this is the way to go when creating a post using php..my questions are:
how do we execute the above php script and where do we save it in the WordPress folder?
what does the 'post_category' array mean(i'd like to use a category id to add posts to WordPress)?
i'd like to mention that i've done some descent searching on the net but the resources i've found do not mention how to execute the above script.
Share Improve this question edited Jan 22, 2014 at 14:41 markratledge 8,7356 gold badges40 silver badges62 bronze badges asked Jan 22, 2014 at 13:10 user11user11 211 silver badge5 bronze badges 2- 1 You mean to say that your descent searching didn't turn up this: codex.wordpress/Function_Reference/wp_insert_post ? – s_ha_dum Commented Jan 22, 2014 at 14:25
- 1 now where do i save that php file in wordpress? – user11 Commented Jan 22, 2014 at 14:37
2 Answers
Reset to default 0Check the docs at http://codex.wordpress/Function_Reference/wp_insert_post for the parameters to use with wp_insert_post
This function inserts posts (and pages) in the database. It sanitizes variables, does some checks, fills in missing variables like date/time, etc. It takes an array as its argument and returns the post ID of the created post (or 0 if there is an error).
You run your function once in the context of a plugin, or worst case, in functions.php or in index.php and then remove it.
From my own searching (also a new PHP user here!) it seems like adding and running code via the Code Snippets plugin is the easiest and safest route for these sorts of one-off code blocks.
This blog gives a good overview of how exactly to add and run PHP code with that plugin: https://www.wpbeginner/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/
本文标签: phpUse of wpinsertpost and parameters
版权声明:本文标题:php - Use of wp_insert_post and parameters 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392195a2466210.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论