Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Contributor Kamal Khan

    (@bhittani)

    Will be upgrading to a new style of code in future with this ability (no promises i’m afraid). Stay tuned

    Thread Starter Kris

    (@omgb)

    cool, thanks

    +1, it would be nice

    Plugin Contributor Kamal Khan

    (@bhittani)

    I found out that you could already order it by url.

    Here is a sample url string:
    https://example.com/?orderby=kksr_avg&order=desc

    The above url will show posts by descending order of avg (highest to lowest). You can also use order=asc for the opposite.

    Thread Starter Kris

    (@omgb)

    thats great!
    for some reason it doesnt work for me though…
    it sorts posts by date
    https://freeandroidgaming.com/

    Bump for this. When i add “?orderby=kksr_avg&order=desc” it shows the normal website showing the most recent posts and the older ones next. Is there a new way now?

    I’m trying trying to figure out a way and i thought even if it’s not possible with a URL query. Is there any way to sort all the posts by the ratings average with a button or something? Like create a button which says “top rated” and make some query which maybe should be custom added on the script or our theme? I don’t really know much about programming this is just a theory. Thanks

    i already have this implemented.

    here you go:

    add to functions.php

    add_filter('query_vars', 'add_vars');
    
    function add_vars($new_query_vars) {
        $new_query_vars[] = 'rating';
        return $new_query_vars;
    }

    now we need to check how the posts are being filtered in pre_get_posts

    function sort_by_rating($query)
    {
    global $wp_query;
    //i have a recipe section using it. change this to either the main posts or any post type archive....
    if($query->is_post_type_archive('recipes') && $query->is_main_query()){
    
       $query_rating = get_query_var('rating'); //checks the rating= in the url
    
            if(isset($query_rating) && $query_rating == 'topr') {
    
                $query->set('meta_key', '_kksr_avg');
                $query->set('orderby', 'meta_value_num');
    
            }
    
            if(isset($query_rating) && $query_rating == 'voted') {
    
                $query->set('meta_key', '_kksr_casts');
                $query->set('orderby', 'meta_value_num');
    
            }
    
        }
    
    }
    add_action('pre_get_posts', 'sort_by_rating');

    Do you just need to copy paste both snippets into “functions.php” ? I’ve tried that and i still can’t use the url query to show the correct results

    Great snippet thanks shamai.

    @mbox99 – worked for me, just by renaming the meta key and changing “$query->is_post_type_archive(‘recipes’) && $query->is_main_query()” to $query->is_archive()

    Sweet! It worked i managed to find a way to make it show the top rated on each category thanks to you ??

    Also how can i achieve the same but on the home page? Repeat the same like the categories. I wanted to make a button to actually show the most top rated pictures in ALL categories instead of just 1 like the snippet. How can i achieve this?

    mbox99 I am looking to sort just my category pages by highest rated posts. I am unsure how to accomplish this. I am using the default app, I am new to this and not sure how to pull this off.

    is this referring to the functions.php of the theme of the wp-include?

    The code shamai posted above i put it into a file called “custom-functions.php” because my theme had a support for that one. But in your case you can put it into “functions.php” ??

    The theme I am using does not use pre_get_posts, instead it calls the_post() function.

    in functions.php

    /***** Loop Output *****/
    
    if (!function_exists('Aloop')) {
    	function Aloop() {
    		if (have_posts()) {
    			while (have_posts()) : the_post();
    				get_template_part('loop', get_post_format());
    			endwhile;
    		} else {
    			get_template_part('content', 'none');
    		}
    	}
    }
    add_action('A_loop_content', 'Aloop');

    Call In index.php

    <?php A_loop_content(); ?>

    From query.php

    function the_post() {
    3492	                global $post;
    3493	                $this->in_the_loop = true;
    3494
    3495	                if ( $this->current_post == -1 ) // loop has just started
    3496	                        /**
    3497	                         * Fires once the loop is started.
    3498	                         *
    3499	                         * @since 2.0.0
    3500	                         *
    3501	                         * @param WP_Query &$this The WP_Query instance (passed by reference).
    3502	                         */
    3503	                        do_action_ref_array( 'loop_start', array( &$this ) );
    3504
    3505	                $post = $this->next_post();
    3506	                setup_postdata($post);
    3507	        }

    I added this to my functions.php I am attempting to get this to only run when someone tries to view any category page.

    /***** RATING SORT ADDITIONS *****/
    add_filter('query_vars', 'add_vars');
    
    function add_vars($new_query_vars) {
        $new_query_vars[] = 'rating';
        return $new_query_vars;
    }
    
    function sort_by_rating($query)
    {
    global $wp_query;
    //i have a recipe section using it. change this to either the main posts or any post type archive....
    if($query->is_archive()){
    
       $query_rating = get_query_var('rating'); //checks the rating= in the url
    
            if(isset($query_rating) && $query_rating == 'topr') {
    
                $query->set('meta_key', '_kksr_avg');
                $query->set('orderby', 'meta_value_num');
    
            }
    
            if(isset($query_rating) && $query_rating == 'voted') {
    
                $query->set('meta_key', '_kksr_casts');
                $query->set('orderby', 'meta_value_num');
    
            }
    
        }
    
    }
    add_action('pre_get_posts', 'sort_by_rating');

    Any ideas on how to attack this setup?

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘is there a way to sort posts by rating with an url query?’ is closed to new replies.