admin管理员组文章数量:1287598
Need to insert custom post type objects from code. Haven't been able to add using the default method
$id = wp_insert_post(array('post_title'=>'random', 'post_type'=>'custom_post'));
creates a regular post instead.
Need to insert custom post type objects from code. Haven't been able to add using the default method
$id = wp_insert_post(array('post_title'=>'random', 'post_type'=>'custom_post'));
creates a regular post instead.
Share Improve this question asked Jul 18, 2013 at 11:09 rashidrashid 2231 gold badge2 silver badges9 bronze badges 3 |5 Answers
Reset to default 18From the Codex:
wp_insert_post() will fill out a default list of these but the user is required to provide the title and content otherwise the database write will fail.
$id = wp_insert_post(array(
'post_title'=>'random',
'post_type'=>'custom_post',
'post_content'=>'demo text'
));
It can be done using the following code :-
To enter a new post for a custom type
$post_id = wp_insert_post(array (
'post_type' => 'your_post_type',
'post_title' => $your_title,
'post_content' => $your_content,
'post_status' => 'publish',
'comment_status' => 'closed', // if you prefer
'ping_status' => 'closed', // if you prefer
));
After inserting the post, a post id will be returned by the above function. Now if you want to enter any post meta information w.r.t this post then following code snippet can be used.
if ($post_id) {
// insert post meta
add_post_meta($post_id, '_your_custom_1', $custom1);
add_post_meta($post_id, '_your_custom_2', $custom2);
add_post_meta($post_id, '_your_custom_3', $custom3);
}
This example worked for me using meta_input
$post_id = wp_insert_post(array (
'post_type' => 'your_post_type',
'post_title' => $your_title,
'post_content' => $your_content,
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'meta_input' => array(
'_your_custom_1' => $custom_1,
'_your_custom_2' => $custom_2,
'_your_custom_3' => $custom_3,
),
));
I found using isset()
allowed me to use wp_insert_post()
on custom post types:
if ( !isset( $id ) ) {
$id = wp_insert_post( $new, true );
}
I had the same problem. I tried every solution provided in most of the forums. But the actual solution that worked for me was the post type was the length of post_type. The post_type length is limited to 20 characters. So anyone who has a similar issue try this if anything else didn't work.
本文标签: wpinsertpost() or similar for custom post type
版权声明:本文标题:wp_insert_post() or similar for custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741267008a2368665.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
custom post type
named ascustom_post
before using this call? – Rohit Pande Commented Jul 18, 2013 at 11:32