wp query - Show one post of each custom taxonomy

admin2025-06-02  1

I am trying to only post one post of each custom taxonomy APP_TAX_STORE my wp_query looks like this:

// show all alternative active coupons for this category from related store

                $tax = get_the_terms($id, 'coupon_category');
                $args = array(
                    'post_type' => APP_POST_TYPE,
                    'post_status' => 'publish',
                    'posts_per_page' => 5,
                    'meta_key' => 'clpr_topcoupon',
                    APP_TAX_TAG => 'gutschein',
                    'orderby'  => array(
                        'meta_value_num' => 'DESC',
                        'post_date'      => 'DESC',
                        ),                      
                    'tax_query' => array( 
                    'relation' => 'AND',
                        array(
                        'taxonomy' => 'coupon_category',
                        'field'    => 'slug',
                        'terms'    => $tax[0]->slug, 
                        ),
                        array(
                        'taxonomy' => APP_TAX_STORE,
                        'field'    => 'slug',
                        'terms'    => $term->slug,
                        'operator' => 'NOT IN',
                         ),
                    ),  
                );
                $query = new WP_Query( $args );
                while ( $query->have_posts() ) :
                    $query->the_post();
                    get_template_part( 'loop', 'coupon' ); 
                endwhile;
                wp_reset_postdata();

Now I need to tell the loop to only show one post of each APP_TAX_STORE (taxonomy).

I do know that I probably need a second query and that I have to combine then. However I tried so many solutions from this forum that I am know totally lost.

With this query I can call one post of each APP_TAX_STORE

$terms = get_terms(array('taxonomy' => APP_TAX_STORE)); 
foreach ($terms as $term){
    $args = array(
    'post_type' => APP_POST_TYPE, 
    'posts_per_page' => '1',
    'order_by' => 'date', 
    'order' => 'ASC', 
    'tax_query' => array(
        array(
            'taxonomy' => APP_TAX_STORE,
            'field' => 'slug',
            'terms' => $term->slug, 
            )
        )
    );
    $new_query = new WP_Query ($args);
    if ($new_query->have_posts()) {
        while($new_query->have_posts()){
            $new_query->the_post();
            get_template_part( 'loop', 'coupon' ); 

        }
    }

}

Now I need to combine these!

Really appreciate some help!

I am trying to only post one post of each custom taxonomy APP_TAX_STORE my wp_query looks like this:

// show all alternative active coupons for this category from related store

                $tax = get_the_terms($id, 'coupon_category');
                $args = array(
                    'post_type' => APP_POST_TYPE,
                    'post_status' => 'publish',
                    'posts_per_page' => 5,
                    'meta_key' => 'clpr_topcoupon',
                    APP_TAX_TAG => 'gutschein',
                    'orderby'  => array(
                        'meta_value_num' => 'DESC',
                        'post_date'      => 'DESC',
                        ),                      
                    'tax_query' => array( 
                    'relation' => 'AND',
                        array(
                        'taxonomy' => 'coupon_category',
                        'field'    => 'slug',
                        'terms'    => $tax[0]->slug, 
                        ),
                        array(
                        'taxonomy' => APP_TAX_STORE,
                        'field'    => 'slug',
                        'terms'    => $term->slug,
                        'operator' => 'NOT IN',
                         ),
                    ),  
                );
                $query = new WP_Query( $args );
                while ( $query->have_posts() ) :
                    $query->the_post();
                    get_template_part( 'loop', 'coupon' ); 
                endwhile;
                wp_reset_postdata();

Now I need to tell the loop to only show one post of each APP_TAX_STORE (taxonomy).

I do know that I probably need a second query and that I have to combine then. However I tried so many solutions from this forum that I am know totally lost.

With this query I can call one post of each APP_TAX_STORE

$terms = get_terms(array('taxonomy' => APP_TAX_STORE)); 
foreach ($terms as $term){
    $args = array(
    'post_type' => APP_POST_TYPE, 
    'posts_per_page' => '1',
    'order_by' => 'date', 
    'order' => 'ASC', 
    'tax_query' => array(
        array(
            'taxonomy' => APP_TAX_STORE,
            'field' => 'slug',
            'terms' => $term->slug, 
            )
        )
    );
    $new_query = new WP_Query ($args);
    if ($new_query->have_posts()) {
        while($new_query->have_posts()){
            $new_query->the_post();
            get_template_part( 'loop', 'coupon' ); 

        }
    }

}

Now I need to combine these!

Really appreciate some help!

Share Improve this question edited Mar 7, 2019 at 15:50 joloshop asked Mar 7, 2019 at 15:11 joloshopjoloshop 211 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

assuming all your constants are correct, it should just look like this

$terms = get_terms(array('taxonomy' => APP_TAX_STORE));
$tax = get_the_terms($id, 'coupon_category');
foreach ($terms as $term) {
    $args = array(
        'post_type'      => APP_POST_TYPE,
        'posts_per_page' => '1',
        APP_TAX_TAG      => 'gutschein',
        'orderby'        => array(
            'meta_value_num' => 'DESC',
            'post_date'      => 'DESC',
        ),
        'tax_query'      => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'coupon_category',
                'field'    => 'slug',
                'terms'    => $tax[0]->slug,
            ),
            array(
                'taxonomy' => APP_TAX_STORE,
                'field'    => 'slug',
                'terms'    => $term->slug,
            ),
        )
    );
    $new_query = new WP_Query ($args);
    if ($new_query->have_posts()) {
        while ($new_query->have_posts()) {
            $new_query->the_post();
            get_template_part('loop', 'coupon');

        }
    }

}

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748832549a314097.html

最新回复(0)