Home>

In the list view, I want to create a list in which regular posts and multiple custom posts are mixed, but I want to exclude only some of the terms from the list.

Current source

<? php
$paged = get_query_var ('paged')? get_query_var ('paged'): 1;
$args = array (
    'post_type' =>array ('post', 'event_info', 'hoge_info', 'fuga_info'), // Specify multiple custom and regular posts
    'paged' =>$paged,
    'order' =>'DESC',
    'posts_per_page' =>-1,
    'tax_query' =>array (
array (
 'taxonomy' =>'event_detail', // Specify taxonomy
 'field' =>'slug',
 'terms' =>'event-post', // specify terms
  'operator' =>'NOT IN',
))
);
$query = new WP_Query ($args);
?>


I want to exclude the term event_post from the event_detail taxonomy in the custom post type event_info.
If i set 'post_type' to single instead of multiple, the specified term will not be displayed, but if you specify it with array, it could not be used.
What is the correct way to write?

  • Answer # 1

    It may not be a direct solution,
    post__not_inMay be used.

    It is a method of specifying and excluding the post ID as follows,
    Get the ID of the term "event_post" of the "event_detail" taxonomy of the custom post type "event_info" in advance as an array,post__not_inIs used to specify

    'post__not_in' =>array (1, 2, 3), // display posts that do not include the array post ID

    Reference URL:
    https://sole-color-blog.com/blog/265/


    'operator' =>'NOT IN',Hopefully this will be a good alternative if you can't proceed with this approach.

  • Answer # 2

    It was a very blind spot, but in the case of a post marked "fixed at the top," it seemed to get past this tax_query, and those not fixed at the top were properly excluded.

    So, use post__not_in which was answered (How to use is completely different, but w)

    'post__not_in' =>get_option ('sticky_posts'), // exclude posts fixed at the beginning


    Was added to the source and was successfully excluded.

    We hope you find it helpful and help someone who is stumbling.