You have to do a slightly more complex query, which will lead you to look for a solution in addition to the main loop (i.e. <?php while ( have_posts() ) : the_post() ; ?>
), but that's not all that troublesome. Come on:
WP makes any type of posts query via WP_Query . The use of the loop as seen in the most basic templates is under the wipes, using the global WP_Query
. In some more specific cases, the global instance does not answer, so you have to create yours.
The key is to know what parameters you should feed the new WP_Query
constructor so that it brings what you want. You need to limit the category (let's assume it has ID
3), and sort by meta_value
s
of a certain meta_key
. This is
$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 this, the core of your loop becomes:
while ($query->have_posts()) {
$query->the_post();
//só partir pro abraço
}
After using a tailor-made query, it is always good to reset the global ones to avoid conflicts. This is done with
wp_reset_postdata();
I believe that you can achieve what you are looking for.
EDIT
As quoted in the comments, I had quoted the incorrect reset method. I have edited the answer so that it is consistent.