WordPress.org

Ready to get started?Download WordPress

Forums

kk Star Ratings
[resolved] Ordering post by kk rating (28 posts)

  1. rarikola
    Member
    Posted 2 years ago #

    First of all Thank you very much for this great plugin.

    I'm trying to reorder posts by kk star rating, not by date.
    I mean each time post is rated, post goes up or down depending on rating.
    so I added custom meta-key to my posts template, and now posts are ordered by meta_key values . like this

    <?php query_posts('meta_key=asd&orderby=meta_value&order=ASC'); ?>

    now I need to pass kk rating to my custom meta_key("asd" in my case) as value for each posts... and I don't know how to do it.

    here is my site http://horror-movies.co/

    Thank you so much for your help.

    http://wordpress.org/extend/plugins/kk-star-ratings/

  2. rarikola
    Member
    Posted 2 years ago #

    I'm guessing i have to use

    <?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?>
    in kk-ratings.php file but I don't know where and how exactly...

    Thanks again for your help...

  3. Kamal Khan
    Member
    Plugin Author

    Posted 2 years ago #

    Hi

    Can you explain this: "now I need to pass kk rating to my custom meta_key("asd" in my case) as value for each posts... and I don't know how to do it."

    I didn't understand this point. Maybe if you brief me about the actual meta_key(instead of asd) and for what purpose you are using it, I may understand better.

    Thanks

  4. rarikola
    Member
    Posted 2 years ago #

    Hello Kamal,

    Thanks for superfast reply :) and sorry for my English.

    Basically what I want to do is to order posts by post rating.
    Usually they are ordered by date.

    Actual meta_key in my case is "asd"...I should give it more descriptive name , like "rating" or something but that's not a point.

    What I want to do is to assign kk-star-rating value for each post, to my meta_key as value., so when user rates movie with say 5 star movie (post) goes up, if user rates with 1 star post it goes down...

    hopefully I explained more clearly this time :)

    Thanks

  5. Kamal Khan
    Member
    Plugin Author

    Posted 2 years ago #

    What you mean is that you just want the post to be ordered by rating (Descending order)? So top rated is at top and lesser rated come below?

  6. rarikola
    Member
    Posted 2 years ago #

    Exactly. That's what I want.

  7. Kamal Khan
    Member
    Plugin Author

    Posted 2 years ago #

    You may want to use this code instead of your current loop.

    if(function_exists(‘kk_star_ratings_get’)) :
        $top_rated_posts = array();
        $top_rated_posts = kk_star_ratings_get(10);
        if(count($top_rated_posts)) :
        wp_reset_query();
        foreach($top_rated_posts as $post)
       {
            $mypost = get_post($post->ID);
            $title = $mypost->post_title;
            $excerpt = $mypost->post_excerpt;
            $permalink = get_permalink($mypost->ID);
            echo $title;
            echo $excerpt;
            echo $permalink;
            wp_reset_query();
        }
        endif;
    endif;

    Hope that works :)

  8. Kamal Khan
    Member
    Plugin Author

    Posted 2 years ago #

    Also, you may want to use this post meta value which is being stored by the plugin for each post.

    _kk_ratings_avg

    This is the post meta containing the average ratings for each post.

    Hope that helps

  9. Kamal Khan
    Member
    Plugin Author

    Posted 2 years ago #

    I guess this will work for you:

    <?php
     query_posts('meta_key=_kk_ratings_avg&orderby=meta_value&order=DESC');
    ?>
  10. rarikola
    Member
    Posted 2 years ago #

    Were exactly should I put that code...here is my file

    <?php query_posts('meta_key=asd&orderby=meta_value&order=ASC'); ?>

    <?php
    
    /**
    
     * This file creates a blog-style layout of posts, useful if you are creating a generic blog.
    
     * This file is not to be loaded directly, but is instead loaded from different templates.
    
     *
    
     * @package Suffusion
    
     * @subpackage Templates
    
     */
    
    global $suffusion, $query_string, $wp_query, $suffusion_current_post_index, $suffusion_full_post_count_for_view, $suffusion_blog_layout, $suffusion_duplicate_posts;
    
    $suffusion_blog_layout = true;
    
    if (!isset($suffusion_duplicate_posts)) $suffusion_duplicate_posts = array();
    
    global $post;
    
    if (have_posts()) {
    
    	$suffusion_current_post_index = 0;
    
    	$suffusion_full_post_count_for_view = suffusion_get_full_content_count();
    
    	while (have_posts()) {
    
    		the_post();
    
    		$original_post = $post;
    
    		if (in_array($post->ID, $suffusion_duplicate_posts)) {
    
    			continue;
    
    		}
    
    		$suffusion_current_post_index++;
    
    		global $suf_category_excerpt, $suf_tag_excerpt, $suf_archive_excerpt, $suf_index_excerpt, $suf_search_excerpt, $suf_author_excerpt, $suf_show_excerpt_thumbnail, $suffusion_current_post_index, $suffusion_full_post_count_for_view, $suf_pop_excerpt, $page_of_posts;
    
    		if (($suffusion_current_post_index > $suffusion_full_post_count_for_view) && ((is_category() && $suf_category_excerpt == "excerpt") ||
    
    			(is_tag() && $suf_tag_excerpt == "excerpt") ||
    
    			(is_search() && $suf_search_excerpt == "excerpt") ||
    
    			(is_author() && $suf_author_excerpt == "excerpt") ||
    
    			((is_date() || is_year() || is_month() || is_day() || is_time())&& $suf_archive_excerpt == "excerpt") ||
    
    			(isset($page_of_posts) && $page_of_posts && $suf_pop_excerpt == "excerpt") ||
    
    			(!(is_singular() || is_category() || is_tag() || is_search() || is_author() || is_date() || is_year() || is_month() || is_day() || is_time()) && $suf_index_excerpt == "excerpt"))) {
    
    			$show_image = $suf_show_excerpt_thumbnail == "show" ? true : false;
    
    			$classes = array('excerpt');
    
    		}
    
    		else {
    
    			$classes = array('full-content');
    
    		}
    
    ?>
    
    	<div <?php post_class($classes);?> id="post-<?php the_ID(); ?>">
    
    <?php
    
    		suffusion_after_begin_post();
    
    ?>
    
    	<div class="entry-container fix">
    
    		<div class="entry entry-content fix">
    
    <?php
    
    		suffusion_content();
    
    ?>
    
    		</div><!--entry -->
    
    <?php
    
    		// Due to the inclusion of Ad Hoc Widgets the global variable $post might have got changed. We will reset it to the original value.
    
    		$post = $original_post;
    
    		suffusion_after_content();
    
    ?>
    
    	</div><!-- .entry-container -->
    
    <?php
    
    		suffusion_before_end_post();
    
    ?>
    
    	</div><!--post -->
    
    <?php
    
    	}
    
    	suffusion_before_end_content();
    
    }
    
    else {
    
    	get_template_part('layouts/template-missing');
    
    }
    
    ?>
  11. rarikola
    Member
    Posted 2 years ago #

    OK I will try and let you know. Thanks so much :)

  12. Kamal Khan
    Member
    Plugin Author

    Posted 2 years ago #

    Most welcome.

    Just use this for querying the post

    query_posts('meta_key=_kk_ratings_avg&orderby=meta_value&order=DESC');

  13. rarikola
    Member
    Posted 2 years ago #

    WOOHOOO ITS WORKING !!! THANK YOU! THANK YOU !THANK YOU!

  14. Kamal Khan
    Member
    Plugin Author

    Posted 2 years ago #

    I am glad it worked. Have fun :)

  15. hearkening
    Member
    Posted 2 years ago #

    This is exactly what I was looking for. Now the only thing is, if a post has not been rated yet, it doesn't show up.

  16. Mr.Halen982
    Member
    Posted 2 years ago #

    That's working for me aswell, thanks!

    One only question is that posts without a rating won't appear anymore. Is there a way to solve this issue?

  17. nelson lopes
    Member
    Posted 1 year ago #

    Hi ,
    First of all ,Congratulations , I love this plugin.

    My only question is the same of that guy above: is that posts without a rating won't appear anymore. Is there a way to solve this issue?

    Thanks

  18. nelson lopes
    Member
    Posted 1 year ago #

    Another alternative could be every time I send a post, it goes with five stars.

    Thanks

  19. Kamal Khan
    Member
    Plugin Author

    Posted 1 year ago #

    Please change this

    query_posts('meta_key=_kk_ratings_avg&orderby=meta_value&order=DESC');

    into

    query_posts('meta_key=_kksr_avg&orderby=meta_value&order=DESC');

  20. Kamal Khan
    Member
    Plugin Author

    Posted 1 year ago #

    all meta keys are changed from _kk_ratings_... to _kksr_...

    So

    _kk_ratings_ratings is now _kksr_ratings
    _kk_ratings_casts is now _kksr_casts
    _kk_ratings_avg is now _kksr_avg

    Thanks

  21. nelson lopes
    Member
    Posted 1 year ago #

    Hi , I`ve made the changes but still the same problem !

    I put the code query_posts('meta_key=_kksr_avg&orderby=meta_value&order=DESC'); on my page and posts without a rating didn`t appear

  22. Kamal Khan
    Member
    Plugin Author

    Posted 1 year ago #

    Hmm, the posts without a rating don't appear because they don't have the meta key of _kksr_avg because they have not yet been rated and yet you are querying for the _kksr_avg.

    This is something custom and can be achieved if you apply a hack to all the previous posts. So a one time hack for all previous posts, applying a meta_key of _kksr_avg with a value of 0. And for all future posts created, you need to place a hook function so that everytime a post is being created, it creates a meta key of _kksr_avg with the value of 0.

    I hope this helps

  23. nelson lopes
    Member
    Posted 1 year ago #

    Thanks for your fast reply.

    Well , I a quite beginner in php.
    When I submitted a post , I know how to get the post`s ID.

    The problem is , how can I add a _kksr_avg = 0 ?

    Thanks again, and that will be my last question !

  24. Kamal Khan
    Member
    Plugin Author

    Posted 1 year ago #

    Paste the code from the below link in your theme folder/functions.php ONLY ONE TIME.

    COPY CODE FROM HERE

    After placing that code in your functions.php file, refresh your wordpress website and then remove that code from functions.php otherwise it will run everytime and will make your website slow.

    This is a quick hack i could provide.

  25. Kamal Khan
    Member
    Plugin Author

    Posted 1 year ago #

    This is the code to paste:

    global $wpdb;
    $Posts = $wpdb->get_results("SELECT ID FROM " . $wpdb->posts);
    foreach ($Posts as $post)
    {
        if(!get_post_meta($post->ID, _kksr_avg, true))
        {
            update_post_meta($post->ID, '_kksr_avg', 0);
        }
    }
  26. nelson lopes
    Member
    Posted 1 year ago #

    Really nice.
    Thanks a million for your help.

    I `ve just saved my life haha !

  27. Kamal Khan
    Member
    Plugin Author

    Posted 1 year ago #

    Glad it worked!

  28. James Griggs
    Member
    Posted 1 year ago #

    I am also trying to have my homepage show posts by the amount of votes they have received, and I cannot figure out where to place the code snippets that you show above. It looks like there are two snippets of code that need to be placed to make this happen, where to do I place each code snippet?

    Thanks for your help.

    [No bumping. If it's that urgent, consider hiring someone. Or try posting your own topic.]

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic