how to display posts per popularity?

[ad_1]

Hi @lola99,

This should help: How to sort posts by views.

If you have any further questions please let me know.

Thread Starter
lola99

(@lola99)

hi

I am not sure how to use this to display most popular posts by category…

Alright, follow steps 1 and 3 from the instructions posted here. When you’re done, add this to your theme’s functions.php file:

/**
 * Sorts posts in category archive by views count.
 *
 * @param  WP_Query  $query
 */
function sort_category_archive_by_views($query) {
    if ( $query->is_main_query() && $query->is_category() )
    {
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'meta_key', 'views_total' );
        $query->set( 'order', 'DESC' );
    }
}
add_action('pre_get_posts', 'sort_category_archive_by_views' );

meta_key should be either views_total (sort by total views count) or views_weekly (sort by most viewed within the last 7 days).

Thread Starter
lola99

(@lola99)

good, it’s working fine, thanks!

Don’t mention it, glad I could help!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer