WP Meta Query Does not Work from template

Viewed 16

Following Query worked fine until 2 weeks ago, no changes were made, but it stopped working. Basically, it loads posts based on meta_value (vl_no), after a lot of testing, it seems if I load the same query from template-content.php (as template from single.php) it doesn't show in the right order, but if I create a Page Template and paste the same code it loads all posts in correct orderby vl_no, below is the code..

$listPosts = get_posts( array(
    'post_type'=> 'post',
    'post_status' => 'publish',
    'numberposts'=> -1,
    'meta_query' => array(
        'key' => 'book_id',
        'value' => $book_id,
        'compare' => '=',
    ),
    'meta_key'=> 'vl_no',
    'orderby' => 'meta_value_num',
    'order'=> 'DESC',
));
    
if ( $listPosts ) : ?>
    <ul class="books">
        <?php   
        foreach ( $listPosts as $post ) : setup_postdata( $post ); 
        ?>              
        <li>
            <h3>
                <a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a>
            </h3>
        </li>
        <?php endforeach; wp_reset_postdata(); ?>
    </ul>
<?php endif; ?>

Can someone please guide what I am doing wrong ? thanks

0 Answers
Related