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
Related
I added two custom fields: day (event_day) & month (event_month) (both of type radio) for CPT Events. Now i want to be able to get posts by meta_key day and month.
The shortcode works except the part with $meta_query.
Here is how shorŠµcode should look like :
[tribe_custom_events_list_mm_wed cat="Rodrigo" num="6" day="Monday" month="October"]
Bellow is the code responsible for the shortcode, added in functions.php
function tribe_custom_events_shortcode($atts, $content = null)
{
global $post;
extract(shortcode_atts(array(
'cat' => '',
'num' => '',
'order' => 'ASC',
'orderby' => 'post_date',
'taxonomy' => 'tribe_events_cat',
'field' => 'name',
'day' => '',
'month' => '',
), $atts));
$tax_query = array(
'taxonomy' => $taxonomy,
'field' => $field,
'terms' => $cat,
);
$day = $day;
$month = $month;
$meta_query = array(
array(
'key' => 'event_day',
'value' => '$day',
'compare' => '='
),
array(
'key' => 'event_month',
'value' => '$month',
'compare' => '='
),
);
$args = array(
'post_type' => 'tribe_events',
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
'tax_query' => array($tax_query),
'meta_query' => array($meta_query),
);
$output = '';
$posts = get_posts($args);
foreach ($posts as $post) {
setup_postdata($post);
$output .= '<div class="tribe-mini-calendar-event event-0 first last">';
$output .= '<h4 class="tribe-events-title">' . get_the_title() . '</h4>';
$output .= '</div>';
}
echo '<pre>' , var_dump($meta_query) , '</pre>';
wp_reset_postdata();
return '<div>' . $output . '</br>' . '</div>';
}
add_shortcode('tribe_custom_events_list_mm_wed', 'tribe_custom_events_shortcode');
This should work for you. There were a few errors in your code... Noted in the comments below.
$tax_query = array(
'taxonomy' => $taxonomy,
'field' => $field,
'terms' => $cat,
);
/* This is unnecessary since $day already = $day
$day = $day;
$month = $month;
*/
$meta_query = array(
array(
'key' => 'event_day',
'value' => $day, // Don't put quotes around variables
'compare' => '='
),
array(
'key' => 'event_month',
'value' => $month,
'compare' => '='
),
);
$args = array(
'post_type' => 'tribe_events',
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
'tax_query' => $tax_query, // This is already an array defined above
'meta_query' => $meta_query,
);
I am trying to exclude posts from query and it is not working at all.
Here what I tried
<?php
$args = array(
'post_type' => 'videos-presentations',
'post_status' => 'publish',
'posts_per_page' => 4,
'paged' => $paged,
'meta_query' => array(
array(
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'meta_compare' => '!='
)
)
);
$my_query = new WP_Query($args);
?>
Also Tried with
'meta_compare' => 'NOT EXIST'
and
'meta_compare' => 'NOT IN'
Any idea what I am doing wrong?
Got it. From here
It Works with just
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'compare' => 'NOT EXISTS'
)
)
function exclude_posts ( $query ) {
$meta_query = $query->get( 'meta_query' );
$meta_query[] = array(
'key'=>'_is_ns_featured_post',
'value'=>'yes',
'compare'=>'!=',
);
$query->set( 'meta_query',$meta_query );
}
add_action( 'pre_get_posts', 'exclude_posts' );
place this code in functions.php file of active theme
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
I'm new into wordpress and kinda confused how to get 1 post in a custom post. I only knew the loop where it echo all the content in a post_type.
I'm aiming to get 1 post from a post_type 'product-category' and the meta_key is 'product-category-and-type'
You could try this query for custom in wordpress:
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
"numberposts" => 1,
'meta_query' => array(
array(
'key' => 'product-category-and-type',
'value' => 'meta_value'
)
)
);
$getPosts = new WP_Query($args);
Just copy and paste below mentioned code at your desired position and replace "your_meta_value" with your actual meta value for meta key "product-category-and-type".
You will get your expected result :
<?php $args = array(
'post_type' => 'product-category',
"numberposts" => 1,
'post_status' => 'publish',
'meta_query' => array(array('key' => 'product-category-and-type','value' => 'your_meta_value'))
);
$myposts = new WP_Query($args);
while($myposts->have_posts()) : $myposts->the_post();
the_title();
endwhile;?>
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
'posts_per_page' => '1',
'tax_query' => array(
array(
'taxonomy' => 'product-category-and-type',
'field' => 'slug'
),
),
);
$result = new WP_Query($args);
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.