You have to make a query a little more complex, which will lead you to seek a solution beyond the loop principal (i.e., <?php while ( have_posts() ) : the_post() ; ?>
), but it’s not that problematic. Come on:
The WP makes any kind of query by posts through the Wp_query. The use of the loop as seen in templates most basic is, behind the scenes, using the global WP_Query
. In some more specific cases, the global instance does not meet, so you have to create your.
The secret lies in knowing which parameters you should feed the new builder WP_Query
for it to bring what you want. You need to limit the category (let’s assume that it has the ID
3), and order by meta_value
s
of a certain meta_key
. That stays
$query = new WP_Query( array(
'meta_key' => 'post_views_count', //a sua meta key
'orderby' => 'meta_value_num',
'order' => 'DESC', //ou ASC, você que escolhe
'cat' => 3
)
);
With that, the core of your loop stays:
while ($query->have_posts()) {
$query->the_post();
//só partir pro abraço
}
After using a bespoke query, it is always good to reset the global ones to avoid conflicts. This is done with
wp_reset_postdata();
I believe this is how you achieve what you seek.
EDIT
As mentioned in the comments, I had cited the incorrect reset method. I edited the answer so that it is coherent.
The correct after a
new WP_Query
iswp_reset_postdata()
– brasofilo
@well noted brasofilo. Reply already edited
– Caio Felipe Pereira