Can't seem to find a definitive answer anywhere. I need to get the ID of a post after it's created with wp_insert_post().
$log_item = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'log-item',
'meta_input' => array(
// 'wpcf-date-checked' => '',
// 'wpcf-checked-by' => '',
'wpcf-belongs-to-id' => $parent_id,
),
);
wp_insert_post( $log_item );
After that, how do I get the ID of the just created $log_item post?
Please store the post id into temporary variable :
$log_item = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'log-item',
'meta_input' => array(
// 'wpcf-date-checked' => '',
// 'wpcf-checked-by' => '',
'wpcf-belongs-to-id' => $parent_id,
),
);
// You can also get the new post ID after inserting a new post:
$post_id = wp_insert_post( $log_item , $wp_error );
For more help : Click Here
Related
I am trying to insert a few fake posts into my WordPress search results. Using the pre_get_posts hook, I am able to get the function to trigger, but I am not able to prepend the fake posts to the WordPress results.
I followed another post about inserting fake posts. The article mention inserting the fake post into the wp-cache.
Any help would be greatly appreciated.
function extra_search_items($query) {
if ($query->is_search && !is_admin()) {
global $wp, $wp_query;
$FakePosts = array(
array(
'ID' => -199,
'post_title' => 'Fake 1',
'post_content' => 'This is a fake virtual post.',
'post_date' => '2018-06-22 00:00:00',
'comment_status' => 'closed',
'post_type' => 'post'
),
array(
'ID' => -200,
'post_title' => 'Fake 2',
'post_content' => 'This is a fake virtual post.',
'post_date' => '2018-06-22 00:00:00',
'comment_status' => 'closed',
'post_type' => 'post'
)
);
$i = 0;
$post = array();
foreach ($FakePosts as $blog) {
// create the post and fill up the fields
$post[$i] = new WP_Post((object)array(
'ID' => $blog['ID'],
'post_title' => $blog['post_title'],
'post_content' => $blog['post_content'],
'post_date' => $blog['post_date'],
'comment_status' => $blog['comment_status'],
'post_type' => $blog['post_type']
));
if(!wp_cache_get($post[$i]->ID, 'posts')) {
wp_cache_set($post[$i]->ID, $post[$i], 'posts');
array_unshift($wp_query->posts, $post[$i]);
$wp_query->post_count++;
}
$i++;
}
}
return $wp_query;
}
add_action('pre_get_posts','extra_search_items');
You created fakeposts array, but you are not combining the fakeposts with with the original posts.
Combine the two arrays and loop it then it will display fakeposts also,
like the following
function extra_search_items($query) {
if ($query->is_search && !is_admin()) {
global $wp, $wp_query;
$FakePosts1 = array(
array(
'ID' => -199,
'post_title' => 'Fake 1',
'post_content' => 'This is a fake virtual post.',
'post_date' => '2018-06-22 00:00:00',
'comment_status' => 'closed',
'post_type' => 'post'
),
array(
'ID' => -200,
'post_title' => 'Fake 2',
'post_content' => 'This is a fake virtual post.',
'post_date' => '2018-06-22 00:00:00',
'comment_status' => 'closed',
'post_type' => 'post'
)
);
$i = 0;
$post = array();
$FakePosts_array = array_merge($FakePosts1,$FakePosts)
foreach ($FakePosts_array as $blog) {
// create the post and fill up the fields
$post[$i] = new WP_Post((object)array(
'ID' => $blog['ID'],
'post_title' => $blog['post_title'],
'post_content' => $blog['post_content'],
'post_date' => $blog['post_date'],
'comment_status' => $blog['comment_status'],
'post_type' => $blog['post_type']
));
if(!wp_cache_get($post[$i]->ID, 'posts')) {
wp_cache_set($post[$i]->ID, $post[$i], 'posts');
array_unshift($wp_query->posts, $post[$i]);
$wp_query->post_count++;
}
$i++;
}
}
return $wp_query;
}
add_action('pre_get_posts','extra_search_items');
I can create a new category using wp_insert_category but I can not add it to my post, any suggestion please!
$cat = array(
'cat_name' => 'dossiers-a-suivre',
'cat_slug' => 'dossiers-a-suivre',
'taxonomy' => 'category' );
$cat_id = wp_insert_category( $cat );
$my_post = array(
'post_title' => "post test",
'post_content' => 'This is my post.',
'post_date' => date('Y-m-d H:i:s'),
'post_type' => 'folder',
'post_category' => array( $cat_id)
);
$post_id = $this->insert_post($my_post);
I solved the problem by using wp_set_object_terms :)
$cat = array(
'cat_name' => 'dossiers-a-suivre',
'cat_slug' => 'dossiers-a-suivre',
'taxonomy' => 'category' );
$cat_id = wp_insert_category( $cat );
$my_post = array(
'post_title' => "post test",
'post_content' => 'This is my post.',
'post_date' => date('Y-m-d H:i:s'),
'post_type' => 'folder',
'category_name' => 'dossiers-a-suivre',
);
$post_id = $this->insert_post($my_post);
wp_set_object_terms($post_id, $cat_id, 'category' );
Try please wp_set_post_terms or wp_set_object_terms
I wrote a script to add new entries into my "interactions" post type, but they don't come up in interactions, and when I manually point my browser to the ID of the post in the editor, it tells me "Unknown post type".
Heres the code I added the post with:
$new_post = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => '',
'post_type' => 'interaction'
);
$id = wp_insert_post($new_post);``
The post is being created but the new entries don't show up on the interactions list and I can't edit it because it tells me its an unknown post type.
What could be causing this issue?
You have an typo in your post_type parameter. Change it to: interactions instead of interaction
<?php
$new_post = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => '',
'post_type' => 'interactions'
);
$id = wp_insert_post($new_post);
When building a WooCommerce site they have made it really easy to display categories and sub categories on the archive and category pages.
However does anyone know if it's possible to add a the list of categories/sub categories to the (content-single-product) page template?
I have a store that had only a handfull of products and we would like users to be able to quick select from the side menu rather than go back and forward between the archive page and the product pages.
Thanks to anyone who can help.
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
$woo_categories = get_categories( $prod_cat_args );
foreach ( $woo_categories as $woo_cat ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_cat_name = $woo_cat->name; //category name
$return .= '' . $woo_cat_name . '';
}
$prod_cat_args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
you can also edit all fields try this
I have the following code in a PHP function that is activated when I install my plugin that lets you create a post or page.
Works perfect and make the page if the $post_type is "post", but if the $post_type is "page", then it does not work, does not create the page.:
$my_post = array(
'post_title' => 'My page Reql',
'post_type' => 'page',
'post_name' => 'my-page',
'post_content' => 'This is my page reql.',
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 1,
'menu_order' => 0
);
wp_insert_post( $my_post );
What is the problem? I can not find the solution.
Thank you very much!
I think you have to set the guid too, like this
$PageGuid = site_url() . "/my-page-req1";
$my_post = array( 'post_title' => 'My page Reql',
'post_type' => 'page',
'post_name' => 'my-page',
'post_content' => 'This is my page reql.',
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 1,
'menu_order' => 0,
'guid' => $PageGuid );
$PageID = wp_insert_post( $my_post, FALSE ); // Get Post ID - FALSE to return 0 instead of wp_error.