Wordpress Custom Field Suite & wp_insert_post - wordpress

$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

Related

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

Prepend fake posts to WordPress search query results

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

WP get posts with no custom field value

I have the following code:
$args = array(
'post_type' => 'epost',
'post_status' => 'future,publish',
'meta_key' => 'custorder',
'orderby'=>'meta_value',
'order' => 'asc',
'posts_per_page' => 900
);
But I can't get the posts on which the custorder value is not set. How can I retrieve those posts also?
You can do this in one query with a proper meta_query
$args = [
'post_type' => 'epost',
'post_status' => ['future','publish'],
'meta_key' => 'custorder',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 900,
'meta_query' => [
[
'key' => 'custorder',
'compare' => 'EXIST'
],
[
'key' => 'custorder',
'compare' => 'NOT EXISTS'
]
]
];
I don't know if is the best solution but I've found a workaround
$query1 = new WP_Query($args1);
$query2 = new WP_Query($args2);
$the_query = new WP_Query();
$the_query->posts = array_merge( $query1->posts, $query2->posts );
$the_query->post_count = $query1->post_count + $query2->post_count;
to match two different arguments into one query. For the first set of arguments I've used 'meta_key'=>'custorder' and for the second one I've added 'meta_compare'=>'NOT EXISTS'
So to find posts of type epost which does not have a meta field with key custorder assigned:
$metaKey = 'custorder';
$postType = 'epost';
$args = [
'post_type' => $postType,
'meta_query' => [
[
'key' => $metaKey,
'compare' => 'NOT EXISTS'
]
]
];
$query = new WP_Query( $args );
Try something like this:
'meta_query' => array(
array(
'key' => 'custorder',
'value' => '',
'compare' => '='
)
),
Source: https://codex.wordpress.org/Class_Reference/WP_Meta_Query

Get value from option tree wordpress

I want to be use some sliders in my wordpress theme. I want to select them in my theme option. I am using the code below.
<?php
$slider_select = get_option_tree( 'slider_select', '', 'true' );
?>
<?php get_template_part('$slider_select'); ?>
But, it is not working. I want the get_template_part code worked. Any suggestion?
replace
get_template_part('$slider_select');
with
get_template_part($slider_select);
You can use this method for create a slider in option tree.
create settings array like this.
array(
'id' => 'my_slider',
'label' => 'Images',
'desc' => '',
'std' => '',
'type' => 'list-item',
'section' => 'general',
'class' => '',
'choices' => array(),
'settings' => array(
array(
'id' => 'slider_image',
'label' => 'Image',
'desc' => '',
'std' => '',
'type' => 'upload',
'class' => '',
'choices' => array()
),
array(
'id' => 'slider_link',
'label' => 'Link to Post',
'desc' => 'Enter the posts url.',
'std' => '',
'type' => 'text',
'class' => '',
'choices' => array()
),
array(
'id' => 'slider_description',
'label' => 'Description',
'desc' => 'This text is used to add fancy captions in the slider.',
'std' => '',
'type' => 'textarea',
'class' => '',
'choices' => array()
)
)
)
Add into page using loop
$my_slider = ot_get_option( 'my_slider' );
foreach($my_slider as $slider){
echo '<img src="'.$slider["slider_image"].'">';
echo $slider["slider_link"];
echo $slider["slider_description"];
}

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