$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - need to exclude APP_TAX_STORE => $term->slug|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin11PV0评论

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

comment list (0)

  1. No comments so far