$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 one post of each custom taxonomy|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 one post of each custom taxonomy

matteradmin10PV0评论

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

        }
    }

}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far