WordPress.org

Ready to get started?Download WordPress

Forums

WP-PostRatings
WP_Query sorting by rating - not showing un-rated posts (5 posts)

  1. alex.powell
    Member
    Posted 1 year ago #

    Hi All.

    I'm sorting a set of posts via WP_Query, by their rating.

    <?
    $args = array(
    	'r_sortby' = 'highest_rated';
    	'r_orderby' = 'desc';
    	);
    $newquery = new WP_Query( $args);
    ?>
    
    <?php while ( $newquery->have_posts() ) : $newquery->the_post(); ?>
    	<?php the_title(); ?>
    	<?php if(function_exists('the_ratings')) { the_ratings(); } ?>
    	<?php the_excerpt(); ?>
    <?php endwhile; wp_reset_query(); ?>

    This correctly shows posts rated highest to lowest... however it omits any unrated posts.

    What I need it to do, is for any unrated posts to show up at the end.

    Turning on WP_DEBUG shows that lines 1088-1092 in wp-postrating.php (in the plugin folder) aren't quite working.

    The offending code

    if(is_null($post_ratings_data)) {
    	$post_ratings_data = get_post_custom($post_id);
    	$post_ratings_users = intval($post_ratings_data['ratings_users'][0]);
    	$post_ratings_score = intval($post_ratings_data['ratings_score'][0]);
    	$post_ratings_average = floatval($post_ratings_data['ratings_average'][0]);
    }

    Does anyone have any suggestions on how to either modifiy my code, to make it work.. or how to modify the plugin's code to make it work

    http://wordpress.org/extend/plugins/wp-postratings/

  2. ruiaires
    Member
    Posted 1 year ago #

    Hi,
    I'm having the same problem.

    It's great to be able to sort posts by using the URL parameters (it works with standard search and archive queries)...

    But I need to show all the posts and not just the ones that are rated.

    One solution might be to manually (via database) rate all the posts but that would not work for newer ones (unless on publish something similar would be programmed)

  3. erikolsen
    Member
    Posted 1 year ago #

    I'm having the same problem. I can get it to do everything I want except it leaves off the un-rated posts.

    Is there any way to query all un-rated posts?

  4. svikramjeet
    Member
    Posted 1 year ago #

    HI
    I'm using following code to sort post according to custom field vote(numeric)

    if ( get_query_var('paged') )
    { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') )
    { $paged = get_query_var('page'); }
    else { $paged = 1; }
    $posts = query_posts($query_string .'&orderby=vote&order=DESC&paged='.$paged);

    BUT THIS CODE IS NOT WORKING....PLEASE HELP ME TO SORT POST USING query_posts(), or if possible by met_key or value i have no idea about "wp_postmeta" table.

  5. mgiulio
    Member
    Posted 1 year ago #

    Hi All,

    The problem is that the code to 'install' the visit counters custom fields is not called because the 'publish_post' and 'publish_page' hooks are not used anymore.

    This is how I patched the plugin(wp-postratings.php file):

    /* This is the original code
    add_action('publish_post', 'add_ratings_fields');
    add_action('publish_page', 'add_ratings_fields');
    function add_ratings_fields($post_ID) {
    	global $wpdb;
    	if(!wp_is_post_revision($post_ID)) {
    		add_post_meta($post_ID, 'ratings_users', 0, true);
    		add_post_meta($post_ID, 'ratings_score', 0, true);
    		add_post_meta($post_ID, 'ratings_average', 0, true);
    	}
    }
    */
    
    // This is the patch
    add_action('new_to_publish', 'add_ratings_fields');
    add_action('draft_to_publish', 'add_ratings_fields');
    add_action('pending_to_publish', 'add_ratings_fields');
    function add_ratings_fields($post) {
    	$post_ID = $post->ID;
    	add_post_meta($post_ID, 'ratings_users', 0, true);
    	add_post_meta($post_ID, 'ratings_score', 0, true);
    	add_post_meta($post_ID, 'ratings_average', 0, true);
    }

    Note that I removed the wp_is_post_revision() test. I got no problems but I am not sure if it is correct. To be sure leave it.

    Another minor issue in my testings were that when the post content was empty(but the title wasn't) the post status transition hooks didn't fire. I don't know why, need investigation...Anyway, it should be an infrequent use case.

    To install the counters in pre-existing posts it is possible to run something like this(use at your own risk and adapt to your case):

    function install_ratings_counters() {
    	$q = new WP_Query(array(
    		//'post_type' => 'XXX',
    		'posts_per_page' => -1
    	));
    
    	while ($q->have_posts()) : $q->the_post();
    		$post_ID = get_the_ID();
    		add_post_meta($post_ID, 'ratings_users', 0, true);
    		add_post_meta($post_ID, 'ratings_score', 0, true);
    		add_post_meta($post_ID, 'ratings_average', 0, true);
    	endwhile;
    }
    
    install_ratings_counters();

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic