• Resolved lola99

    (@lola99)


    Hello

    I would like to show posts ordered per popularity all time or 7 last days, what comes first, in every category instead of having posts by date they are published.
    I used Generatepress theme and your plugin for popularity but i can’t find the way to sort my posts
    Do you know how i can do that?
    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    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…

    Plugin Author Hector Cabrera

    (@hcabrera)

    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!

    Plugin Author Hector Cabrera

    (@hcabrera)

    Don’t mention it, glad I could help!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘how to display posts per popularity?’ is closed to new replies.