POSTS list in WordPress by views

Seems like you already have the custom field for views set up since that’s a phpMyAdmin screenshot. If so, you can use order_by="post_views_count" in most wp_query based calls. For instance:

$args = array(
    'post_type' => 'your_post_type',
    'orderby'   => 'meta_value_num',
    'meta_key'  => 'post_views_count',
);
$query = new WP_Query( $args );

//...and on with the loop
<?php while ( have_posts() ) : the_post(); ?>...

Limiting this behavior to category 3 is pretty easy but how go about it depends on the context and your template setup.

More on orderby in the WP Codex

Leave a Comment