最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - How to display all posts not in a post_format

programmeradmin1浏览0评论

Using WP_Query() I want to display all posts -except- those with the 'image' post_format. I tried this, but it did not work.

    $args = array(
      'relation' => 'NOT',      
      'tax_query' => array(
    array(
        'taxonomy' => 'post_format',
        'field' => 'slug',
        'terms' => array('post-format-' . $post_format),
      )
    ),
        'paged' => get_query_var('paged')
    );
    query_posts( $args );  

Using WP_Query() I want to display all posts -except- those with the 'image' post_format. I tried this, but it did not work.

    $args = array(
      'relation' => 'NOT',      
      'tax_query' => array(
    array(
        'taxonomy' => 'post_format',
        'field' => 'slug',
        'terms' => array('post-format-' . $post_format),
      )
    ),
        'paged' => get_query_var('paged')
    );
    query_posts( $args );  
Share Improve this question edited Oct 20, 2020 at 18:36 jchwebdev asked Oct 20, 2020 at 17:35 jchwebdevjchwebdev 7752 gold badges14 silver badges33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

It didn't work because your tax query is missing the operator which should be set to NOT IN. So just add it like so:

'tax_query' => array(
    array(
        'taxonomy' => 'post_format',
        'field'    => 'slug',
        'terms'    => array( 'post-format-' . $post_format ),
        'operator' => 'NOT IN',
    )
),

And please, avoid using query_posts() — use new WP_Query() or get_posts() instead — or hooks such as pre_get_posts for modifying the query parameters.

发布评论

评论列表(0)

  1. 暂无评论