wp query - need to exclude APP_TAX_STORE => $term->slug

admin2025-06-03  3

I have this code:

query_posts( array(
                        'post_type' => APP_POST_TYPE,
                        'post_status' => 'publish',
                        'posts_per_page' => 4,
                        ),                      
                    ) );

now I need to remove all post from this Query APP_TAX_STORE => $term->slug

Meaning I need to show all posts except the one from this one taxonomy.

I have this code:

query_posts( array(
                        'post_type' => APP_POST_TYPE,
                        'post_status' => 'publish',
                        'posts_per_page' => 4,
                        ),                      
                    ) );

now I need to remove all post from this Query APP_TAX_STORE => $term->slug

Meaning I need to show all posts except the one from this one taxonomy.

Share Improve this question edited Feb 2, 2019 at 21:19 joloshop asked Feb 2, 2019 at 21:04 joloshopjoloshop 211 silver badge8 bronze badges 2
  • Is the term a standard WordPress category or tag, or is a custom taxonomy? – phatskat Commented Feb 2, 2019 at 23:46
  • its custom taxonomy – joloshop Commented Feb 3, 2019 at 0:02
Add a comment  | 

2 Answers 2

Reset to default 1

The easiest way I can see is to gather a list of the terms in that taxonomy, and use them with a NOT IN operator in your query's tax_query:

// Get all the terms in your custom taxonomy.
$slugs = get_terms( [
    'taxonomy' => 'industry',
    'fields'   => 'slugs',
] );

$posts = query_posts( [
    'post_type'      => 'post',
    'posts_per_page' => 4,
    'post_status'    => 'publish',
    'tax_query'      => [
        [
            'taxonomy' => APP_TAX_STORE,
            'operator' => 'NOT IN',
            'field'    => 'slug',
            'terms'    => $slugs, 
        ]
    ]
] );

I would suggest reading up on query_posts if you haven't already - heed the warning that it could cause confusion and trouble down the road. get_posts should get you the same results without the burden of overwriting the main query.

Okay found a solution that's working as well:

query_posts( array(
  'post_type'      => APP_POST_TYPE,
  'post_status'    => 'publish',
  'posts_per_page' => 4,
  'tax_query'      => array(
    array(
      'taxonomy' => APP_TAX_STORE,
      'field'    => 'slug',
      'terms'    => $term->slug,
      'operator' => 'NOT IN',
    ),
  ),
) );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748948279a315071.html

最新回复(0)