$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 - Show posts without term|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 - Show posts without term

matteradmin9PV0评论

I am working on a product page where I show reviews from my post type 'reviews', if the product page title matches with my term from the taxonomy 'review-product'.

Example.

Product page title: Refrigerator

Show (reviews) posts with:

  • Post type: 'reviews'
  • Taxonomy: 'review-product'
  • Term: Refrigerator

This works totally fine. But sometimes I have no reviews for a product (because there is no match between product title and term) and then I want to show 'General' reviews. In my website these are posts with:

  • Post type: 'reviews'
  • Taxonomy: 'review-product'
  • Term: No term (!!!)

This posts don't have a term from the taxonomy 'review-product'.

My Question:

How can I show reviews posts without a term, if there are no reviews posts matching the product title.

This is what I have now:

function gtp_show_reviews( $number = 100, $term = null ) {
    $reviews = new WP_Query( array( 
        'post_type'         => 'reviews',
        'review-product'    => $term, // Fill this with product page slug (refrigerator)
    ));
    if( $reviews->have_posts() ) { 
        while( $reviews->have_posts() ) {
            $reviews->the_post();
            // The review
        }
    } 
    else {
        // Here I try to unset the 'review-product' term
        unset($reviews->query_vars['review-product']);
        unset($reviews->query_vars['term']);

        while( $reviews->have_posts() ) {
            $reviews->the_post();
            // Check if review has no term
            if( $reviews->has_term() == false ) {
               // The review
            }
        }   
    }

    wp_reset_postdata();
}

I am working on a product page where I show reviews from my post type 'reviews', if the product page title matches with my term from the taxonomy 'review-product'.

Example.

Product page title: Refrigerator

Show (reviews) posts with:

  • Post type: 'reviews'
  • Taxonomy: 'review-product'
  • Term: Refrigerator

This works totally fine. But sometimes I have no reviews for a product (because there is no match between product title and term) and then I want to show 'General' reviews. In my website these are posts with:

  • Post type: 'reviews'
  • Taxonomy: 'review-product'
  • Term: No term (!!!)

This posts don't have a term from the taxonomy 'review-product'.

My Question:

How can I show reviews posts without a term, if there are no reviews posts matching the product title.

This is what I have now:

function gtp_show_reviews( $number = 100, $term = null ) {
    $reviews = new WP_Query( array( 
        'post_type'         => 'reviews',
        'review-product'    => $term, // Fill this with product page slug (refrigerator)
    ));
    if( $reviews->have_posts() ) { 
        while( $reviews->have_posts() ) {
            $reviews->the_post();
            // The review
        }
    } 
    else {
        // Here I try to unset the 'review-product' term
        unset($reviews->query_vars['review-product']);
        unset($reviews->query_vars['term']);

        while( $reviews->have_posts() ) {
            $reviews->the_post();
            // Check if review has no term
            if( $reviews->has_term() == false ) {
               // The review
            }
        }   
    }

    wp_reset_postdata();
}
Share Improve this question asked Aug 27, 2014 at 12:55 RobbertRobbert 1,2756 gold badges23 silver badges47 bronze badges 1
  • This is going to be an expensive NOT IN query, you should always query for what you want, not what you don't want – Tom J Nowell Commented May 2, 2017 at 15:17
Add a comment  | 

2 Answers 2

Reset to default 13

You cannot use the same object of WP_Query twice. Therefore you need to create another one with a tax_query parameter to fetch posts which are not assigned to any term.

//fetch all reviews which have no assigned term in 'review-product'
$taxonomy  = 'review-product';
$post_type = 'reviews';
$args = [
    'post_type' => $post_type,
    'tax_query' => [
        [
            'taxonomy' => $taxonomy,
            'terms'    => get_terms( $taxonomy, [ 'fields' => 'ids'  ] ),
            'operator' => 'NOT IN'
        ]
    ]
];

$query = new \WP_Query( $args );

The idea is to fetch a list of all terms of your taxonomy and pass them as argument to your tax-query with the NOT IN operator.

The second loop in your example should walk over the new WP_Query object.

$posts_query = new WP_Query( array(
    'tax_query' => array(
        array(
            'taxonomy' => 'foo',
            'operator' => 'NOT EXISTS', // or 'EXISTS'
        ),
    ),
) );

see: https://core.trac.wordpress/ticket/29181

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far