php – orderby meta_value_num is not working, giving default order
Question
I am trying to order my posts by a meta value so that I can show the most popular posts. But it is returning the latest posts instead.
//WordPress loop for the custom post type
$popular_args = array(
'post_type' => 'games',
'posts_per_page' => 6,
'suppress_filters' => true, //Just in case something else is interfaring.
'orderby' => 'meta_value_num',
'meta_key' => 'view_54ygtt',
'order' => 'DESC',
'meta_query' => array(
array(
'type' => 'NUMERIC'
)
),
);
$popular_query = new WP_Query($popular_args);
while ($popular_query->have_posts()) : $popular_query->the_post();
//Get the template
echo get_the_title() .' - '. get_post_meta($post->ID, 'view_54ygtt', true) .'<br>';
endwhile;
wp_reset_query(); ?>
Result:
Sample Post 6 – 1282
Sample Post 5 – 25719
Sample Post 4 – 88999
Sample Post 3 – 77354
Sample Post 2 – 7948
Sample Post 1 – 1526
It should be:
Sample Post 4 – 88999
Sample Post 3 – 77354
Sample Post 5 – 25719
Sample Post 2 – 7948
Sample Post 1 – 1526
Sample Post 6 – 1282
0
2 weeks
2022-08-05T19:55:23-05:00
2022-08-05T19:55:23-05:00 0 Answers
0 views
0
Leave an answer