• First off I’d like to commend this plugin. It’s really the best I’ve used on my site
    I’d like to sort my posts by highest rated on the front page. I saw that someone already raised an issue like this on the group and a code was provided. I tried it and it worked but there’s still a modification I’m in need of.
    For instance two posts have been each rated 5 stars and one of the posts has a high number of people voting than the other, I’d like the post with the higher visitor votes to precede the other.

    Below is the code that worked for me

    `add_filter(‘posts_join_paged’,’yasr_join_paged’);
    add_filter(‘posts_where’, ‘yasr_where’);
    add_filter(‘posts_groupby’, ‘yasr_groupby’);
    add_filter(‘posts_orderby’, ‘yasr_posts_orderby’);

    function yasr_join_paged($join_paged_statement) {
    if(is_home() && is_main_query()) {
    global $wpdb;
    return “, {$wpdb->prefix}yasr_log AS YL”;
    }
    return $join_paged_statement;
    }

    function yasr_where($where) {
    if(is_home() && is_main_query()) {
    global $wpdb;
    return ” AND YL.post_id = {$wpdb->prefix}posts.ID”;
    }
    return $where;
    }

    function yasr_groupby($groupby) {
    if(is_home() && is_main_query()) {
    global $wpdb;
    return ” YL.post_id, {$wpdb->prefix}posts.ID”;
    }
    return $groupby;
    }

    function yasr_posts_orderby($orderby_statement) {
    if(is_home() && is_main_query()) {
    return ” (ROUND(SUM(YL.vote) / COUNT(YL.post_id), 1)) DESC”;
    }
    return $orderby_statement;}

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor dudo

    (@dudo)

    Hello!

    Try to change the last function with this:

    function yasr_posts_orderby($orderby_statement) {
        if (is_main_query()) {
            return " (ROUND(SUM(YL.vote) / COUNT(YL.post_id), 1)) DESC, COUNT(YL.post_id) DESC";
        }
        return $orderby_statement;
    }
    

    Let me know!

    Thread Starter credon

    (@credon)

    It arranges posts on my post list accordingly but it shows errors on all my posts

    Plugin Contributor dudo

    (@dudo)

    Hi, sorry, really busy days and I can’t test.

    What error do you have?

    Thread Starter credon

    (@credon)

    Hi
    It’s okay
    I’ve fixed it
    I add the is_home function and it worked
    But the issue I have is with the fake votes
    It duplicates the posts and the duplicate has an error

    Plugin Contributor dudo

    (@dudo)

    This is a pro feature, and due to www.ads-software.com forum rules I can’t discuss it here.

    Can you please email me?

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to sort posts by highest rated’ is closed to new replies.