1

I need to populate the terms parameter in my tax_query with the current term of the post.

I have been trying to use the WP function wp_get_post_terms in a variable and then referencing that variable in the terms parameter to input the current posts term name.

I have been using the codex page as a reference, but I cannot seem to get it to populate.

Can anyone point me in the right direction??

//Returns Array of Term Names for "topic"
    $term_list = wp_get_post_terms($post->ID, 'topic', array("fields" => "names"));
    echo $term_list;

    $args = array (
        'post_type'      => 'knowledge-base',
        'orderby'        => 'meta_value_num', 
        'meta_key'       => 'top_four_num',
        'tax_query' => array(
        array(
            'taxonomy' => 'topic',
            'field'    => 'slug',
            'terms'    => $term_list,                                       

            ),
        ),                  
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();

            echo '<h2>' . the_title() . '</h2>';

        }
    } 

    // Restore original Post Data
    wp_reset_postdata();

Any help is much appreciated!

1

You have a couple of issues here.

  • As your code stand, $post is undefined. It is actually better to make use of get_the_ID() to get the post ID instead of $post

  • wp_get_post_terms() makes an extra db call, so if you are really set on performance, I would rather use get_the_terms()

  • You are returning term names, but then set the field parameter in your tax_query to slug. The field value should match the value of the term passed. Just a note, never ever use the name field in a tax_query, there is an issue with name sanitation in the WP_Tax_Query class. If you are using wp_get_post_terms(), set the fields parameter to ids to return an array of term ids

EXAMPLES

wp_get_post_terms()

$term_list = wp_get_post_terms(
    get_the_ID(), 
    'topic', 
    array(
        'fields' => 'ids'
    )
);

if (    $term_list
     && !is_wp_error( $term_list )
) {
    $args = array (
        'post_type'      => 'knowledge-base',
        'orderby'        => 'meta_value_num', 
        'meta_key'       => 'top_four_num',
        'tax_query' => array(
            array(
                'taxonomy' => 'topic',
                'terms'    => $term_list,                                       
            ),
        ),                  
    );
    // Run your custom query here
}

get_the_terms()

$terms = get_the_terms(
    get_the_ID(), 
    'topic'
);

if (    $terms
     && !is_wp_error( $terms )
) {
    $term_list = wp_list_pluck( $terms, 'term_id' );

    $args = array (
        'post_type'      => 'knowledge-base',
        'orderby'        => 'meta_value_num', 
        'meta_key'       => 'top_four_num',
        'tax_query' => array(
            array(
                'taxonomy' => 'topic',
                'terms'    => $term_list,                                       
            ),
        ),                  
    );
    // Run your custom query here
}
| improve this answer | |
  • 1
    Ah that makes perfect sense!!! Thanks for the detailed explaination.. My code is now working as expected!! :) – tmgale12 Jul 5 '16 at 8:03
  • My pleasure, glad it worked ;-) – Pieter Goosen Jul 5 '16 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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