1

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.

  • Is the term a standard WordPress category or tag, or is a custom taxonomy? – phatskat Feb 2 at 23:46
  • its custom taxonomy – joloshop Feb 3 at 0:02
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.

  • Thank you its working! However I do not need the // Get all the terms in your custom taxonomy. $slugs = get_terms( [ 'taxonomy' => 'industry', 'fields' => 'slugs', ] ); part of it. What was the reason for it? Also it fixed another problem I had. It is now only showing posts from the same category. Thats what I wanted as well. Now I need a fix to only show one post from each taxonomy APP_TAX_STORE. – joloshop Feb 3 at 13:53
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.