mimi mimi - 3 months ago 16
PHP Question

wp_query return empty array

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.

Answer

Change this line

loop = new WP_Query( $args );

To

$loop = new WP_Query( $args );

The full code

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

    $term_list = wp_get_post_terms($post_id, 'best-book', array("fields" => "all"));   
    foreach($term_list as $term_single){
         ------Your Code Goes Here------ 
   }  
?>