0

I have custom post type 'book' and I also make taxonomy name best-book which. I want to filter taxonomy term but it doesn't work. I try to put echo and print_r to find where is my problem and I found that my $loop var return empty array. I also use this style in other pages and never have problem but I can't find why this happen? and how can I have fix it. here is the photo which show it return empty array enter image description here here is my code

<?php $args = array( 'post_type'=>'book'); 
    loop = new WP_Query( $args );
    print_r($loop);
    while ( have_posts() ) : the_post();
    $post=the_post();

    $term_list = wp_get_post_terms($post_id, 'best-book', array("fields" => "all"));

    foreach($term_list as $term_single) 
}

?>

any idea would be appreciated.

0

You need to reference to your query, when starting the loop, otherwise it will show the posts found with the main query, instead of your custom query.

Try:

<?php

    $args = array( 'post_type'=>'book'); 
    $loop = new WP_Query( $args );

    // Start the loop for your custom query
    if($loop->have_posts() ) : while ($loop->have_posts() ) : $loop->the_post();

    the_title(); // just an example, output what you need here

    // Get the post terms
    $term_list = wp_get_post_terms($post->ID, 'best-book', array("fields" => "all"));

    // Iterate the terms, if found
    if($term_list) {
        foreach($term_list as $term_single) {
            echo $term_single;
        }
    }

    endwhile;

    // In case the query is empty
    else:

    // Debug the query
    echo '<pre>';
    var_dump($loop);
    echo '</pre>';

    endif;

?>

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.