Prepend fake posts to WordPress search query results - wordpress

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');

Related

wp_insert_post / wp_update_post Duplications

I'm importing from a JSON file and I get duplicates every time the importer runs.
The foreach
$name = $data->name;
$desc = $data->description;
$type = $data->type;
$tutor = $data->tutor;
$location = $data->location;
$planned_start = $data->planned_start;
$actual_start = $data->actual_start;
$actual_end = $data->actual_end;
$max_attendees = $data->max_attendees;
The importer
$check_title = get_page_by_title( $name);
$my_post = array(
'ID' => $check_title->ID,
'post_title' => wp_strip_all_tags($name),
'post_type' => 'training',
'post_content' => $desc,
'post_status' => 'publish',
'post_author' => 1,
'comment_status' => 'closed',
'ping_status' => 'closed',
'meta_input' => array(
'course_type' => $type,
'tutor' => $tutor,
'location' => $location,
'planned_start' => $planned_start,
'actual_start' => $actual_start,
'actual_end' => $actual_end,
'max_attendees' => $max_attendees,
),
'tax_input' => array(
'custom_cat_training' => $type,
),
);
if (empty($check_title)){
$post_id = wp_insert_post($my_post);
wp_set_object_terms($post_id, $type, 'custom_cat_training');
} else {
$post_id = wp_update_post($my_post);
wp_set_object_terms($check_title->ID, $type, 'custom_cat_training');
}
I'm probably missing something obvious but I can't for the life of me work it out.
you can check it with it
https://developer.wordpress.org/reference/functions/post_exists/
if (!post_exists($name)){
$post_id = wp_insert_post($my_post);
wp_set_object_terms($post_id, $type, 'custom_cat_training');
} else {
$post_id = wp_update_post($my_post);
wp_set_object_terms($check_title->ID, $type, 'custom_cat_training');
}
I used get_page_by_path($trimmed, OBJECT, 'training');
and used the page slug rather than title

Wordpress Custom Field Suite & wp_insert_post

$post_data = [
'post_type' => 'messages',
'post_title' => $_POST['subject'],
'post_content' => $_POST['message'],
'post_status' => 'publish',
'meta_input' => [
'???' => $_POST['name'],
'???' => $_POST['email']
]
];
$post_id = wp_insert_post(wp_slash($post_data));
I'm using Custom Field Suite plugin to create custom fields. What should I use instead of ???
Got it:
$post_data = [
'post_type' => 'messages',
'post_title' => $_POST['subject'],
'post_content' => $_POST['message'],
'post_status' => 'publish'
];
$post_id = wp_insert_post(wp_slash($post_data));
CFS()->save([
'name' => $_POST['name'],
'email' => $_POST['email']
], [
'ID' => $post_id
]);
More information in the docs

Get ID of post after creation with wp_insert_post()

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

Wp_insert_post does not add post category

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

Create a new page with wp_insert_post ()

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.

Resources