0

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!

1 Answer 1

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

        }
    }

}

1
  • Thats working, however it now shows all posts within this category and not just 5. ;-(. The man reason for this query is to not have two post from the same APP_TAX_STORE taxonomy
    – joloshop
    Commented Mar 7, 2019 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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