$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 - query_posts problem - need help|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 - query_posts problem - need help

matteradmin8PV0评论

I have a taxonomy called coupon_category. In a query_post I am trying to call all posts from related custom taxonomy with the same coupon_category.

If I use:

<?php

                    // show all active coupons for this category from related store and setup pagination

                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    query_posts( array(
                        'post_type' => APP_POST_TYPE,
                        'post_status' => 'publish',
                        'posts_per_page' => 4,
                        'tax_query' => array( 
                            array(
                            'taxonomy' => 'coupon_category', 
                            'field'    => 'slug',
                            'terms'    => 'mode', 
                            ),
    )
                    ) );                
                ?>

I can show all related posts with the term "mode" however I would like to automate it, so that always the terms (coupon_category) that are already in use on the page are shown.

I have a taxonomy called coupon_category. In a query_post I am trying to call all posts from related custom taxonomy with the same coupon_category.

If I use:

<?php

                    // show all active coupons for this category from related store and setup pagination

                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    query_posts( array(
                        'post_type' => APP_POST_TYPE,
                        'post_status' => 'publish',
                        'posts_per_page' => 4,
                        'tax_query' => array( 
                            array(
                            'taxonomy' => 'coupon_category', 
                            'field'    => 'slug',
                            'terms'    => 'mode', 
                            ),
    )
                    ) );                
                ?>

I can show all related posts with the term "mode" however I would like to automate it, so that always the terms (coupon_category) that are already in use on the page are shown.

Share Improve this question edited Feb 2, 2019 at 18:37 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 2, 2019 at 16:21 joloshopjoloshop 211 silver badge8 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

You'll have to get the terms of current post with get_the_terms function and then use them in your query.

// this will get terms and then get only term_ids from them
$term_ids = wp_list_pluck( get_the_terms( get_the_ID(), 'coupon_category' ), 'term_id' );

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$related = new WP_Query( array(
    'post_type' => APP_POST_TYPE,
    'post_status' => 'publish',
    'posts_per_page' => 4,
    'tax_query' => array( 
        array(
            'taxonomy' => 'coupon_category', 
            'field'    => 'term_id',
            'terms'    => $term_ids, 
        ),
    )
) ); 

So we use get_the_terms to get list of terms for current post and then we call wp_list_pluck, which is a very handy function, that will get array containing only one field from all of given objects.

PS. I've also changed your query_posts to WP_Query - it's better for your efficiency.

Okay after a lot of reading I finally changed to 'wp_query' and this is the perfect solution:

$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();

Found a solution, quite similar to yours:

$tax = get_the_terms($id, 'coupon_category');
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    query_posts( array(
                        'post_type' => APP_POST_TYPE,
                        'post_status' => 'publish',
                        'posts_per_page' => 4,
                        'tax_query' => array( 
                            array(
                            'taxonomy' => 'coupon_category', // Taxonomy
                            'field'    => 'slug',
                            'terms'    => $tax[0]->slug, // Your category slug
                            ),
    )

                    ) );

Now I only need to exclude post from APP_TAX_STORE with current slug or from taxonomy stores.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far