WordPress.org

Ready to get started?Download WordPress

Forums

WP-PostRatings
[resolved] URL Query string to order posts by highest score missing posts (24 posts)

  1. KS Web Designer
    Member
    Posted 8 months ago #

    I am trying to use the URL query string to order my posts by those with the highest score (http://yoursite.com/?r_sortby=highest_rated&r_orderby=desc). It mostly works except it's missing some posts. It doesn't seem to include those that haven't received a rating at all.

    Is there any way to include these posts too, as I'd like to order all posts.

    Thanks

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

  2. Lester Chan
    Member
    Plugin Author

    Posted 8 months ago #

  3. KS Web Designer
    Member
    Posted 8 months ago #

    Hmmm, yeah. I have tested on the theme twentythirteen (just to make sure it wasn't something I introduced in my custom theme), but the same issue occurs. The only thing I can think of is that it's a problem with custom post types, as that's what's being displayed...?

  4. Lester Chan
    Member
    Plugin Author

    Posted 8 months ago #

    Another possibility is that those are old posts created before wp-postratings got activated

  5. KS Web Designer
    Member
    Posted 8 months ago #

    Good thinking, but sadly that's not it either. It was a possibility though. I created a new item, didn't give it any votes and it isn't showing up when viewing by highest rated (as above). But it appears when the sorting is not applied.

  6. Lester Chan
    Member
    Plugin Author

    Posted 8 months ago #

    That is weird could you check the new post to see if there are 3 new custom field being created? ratings_average, ratings_users and ratings_score?

  7. KS Web Designer
    Member
    Posted 8 months ago #

    I have had a look through phpMyAdmin. There are records for ratings_score in the wp_postmeta table (I presume this is the correct place?)

    However there is no record for my new post, again, only those that have had a rating applied (either positive or negative).

  8. Lester Chan
    Member
    Plugin Author

    Posted 8 months ago #

    Yes, I found the issue, it is because you are using custom post type and when you create a new post, the ratings will not get inserted because it is a custom post type.

    You can add this code after line 542 of wp-postratings.php https://github.com/lesterchan/wp-postratings/blob/master/wp-postratings.php#L542

    add_action('publish_<CUSTOM_POST_TYPE_NAME>', 'add_ratings_fields_custom');
    function add_ratings_fields_custom($id) {
            if(!wp_is_post_revision($id)) {
                    add_metadata('<CUSTOM_POST_TYPE_NAME>', $id, 'ratings_users', 0, true);
                    add_metadata('<CUSTOM_POST_TYPE_NAME>', $id, 'ratings_score', 0, true);
                    add_metadata('<CUSTOM_POST_TYPE_NAME>', $id, 'ratings_average', 0, true);
            }
    }

    The code is not tested, so I am not too sure if it works.

  9. KS Web Designer
    Member
    Posted 8 months ago #

    Thanks, I gave it a go but it caused the following error on my pages:

    Cannot redeclare add_ratings_fields() (previously declared in /home/mti/public_html/wp-content/plugins/wp-postratings/wp-postratings.php:535)

  10. Lester Chan
    Member
    Plugin Author

    Posted 8 months ago #

    I edited the post with the correct code, I pasted too fast

  11. KS Web Designer
    Member
    Posted 8 months ago #

    Sorry, that still doesn't work. The error I posted before is gone but when creating a new custom post it's still not getting the three custom fields created (I checked the Database again after creating two new posts).

  12. Lester Chan
    Member
    Plugin Author

    Posted 8 months ago #

    Stupid question, you did replace <CUSTOM_POST_TYPE_NAME> with the post type name right?

    Maybe you can remove the if(!wp_is_post_revision($id)) check. I can only guess because I don't use Custom Post Type myself and this plugin (as the name suggest) is meant for pages or post.

  13. KS Web Designer
    Member
    Posted 8 months ago #

    Indeed I did swap that text (all 4 instances), but worth checking for silly errors!

    I tried your other suggestion but it made no difference.

    Then I started tinkering. My knowledge on PHP is minimal but I thought I'd just try merging your new code with the existing code on line 532. I changed it from this:

    ### Function: Add Rating Custom Fields
    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);
    	}
    }

    To this:

    ### Function: Add Rating Custom Fields
    add_action('publish_post', 'add_ratings_fields');
    add_action('publish_page', 'add_ratings_fields');
    add_action('publish_<CUSTOM_POST_TYPE_NAME>', '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);
    	}
    }

    And that seemed to do the trick. I just figured the two functions were largely the same, and the way WordPress handles custom post types is mostly the same as posts or pages.

    I know you're unable to test the code Lester, but is there anythign with this change that I should be careful of? As I say it all seems to work, but just in case there's a specific reason you didn't attempt something along these lines?

    Thanks

  14. Lester Chan
    Member
    Plugin Author

    Posted 8 months ago #

    I am surprised that add_post_meta() works. add_post_meta() calls add_metadata('post', $post_id, $meta_key, $meta_value, $unique) which means it is a post type, that is the reason why I called add_metadata directly and passing in the < CUSTOM_POST_TYPE_NAME>.

  15. KS Web Designer
    Member
    Posted 8 months ago #

    Oh I see. Well I know when I specified a custom post type I based it on the basic functionality of a 'post' so perhaps that's the reason? If the underlying functionality matches a post then that could explain why this works?

    The other thing is that as far as I was aware all items have a $post_id, regardless of type? I may be wrong on that, and perhaps this falls under the same thing of my custom type being based on a 'post'.

    What do you think?

  16. Lester Chan
    Member
    Plugin Author

    Posted 8 months ago #

    I did not really dig into much of WordPress custom post type yet so I can't answer that. What is the code you use to defined the custom post type, using register_post_type()?

  17. KS Web Designer
    Member
    Posted 8 months ago #

    I'm afraid I couldn't tell you - I cheat and use a plugin for my custom post types, makes it easier to manage having a nice interface to deal with the custom fields and all that.

    Sorry. I can give you the name of the plugin though, and the guy that made it is incredibly helpful and would probably be happy to give you some advice for getting yours to work with custom posts.

  18. Lester Chan
    Member
    Plugin Author

    Posted 8 months ago #

    I see, no worries, thank you =D

  19. KS Web Designer
    Member
    Posted 8 months ago #

    No problem - thanks for all your help :)

  20. fredje007
    Member
    Posted 7 months ago #

    What exactly did you replace < CUSTOM_POST_TYPE_NAME> with?
    I tried you solution here, and replaced it with what I know that name name of my custom post type is. (url structure post types is: ....post_type="<custom_post_type_name>") But in the Widget Is still only see "Include ratings from: posts & pages, posts only, pages only".

    Maybe I'm still missing a step...

    Could you help?

    I created the custom post types using the TYPES plugin.

  21. KS Web Designer
    Member
    Posted 7 months ago #

    I replaced <CUSTOM_POST_TYPENAME> with the name of my custom post type. So let's say you made a new custom type called 'shop_item' the code I posted above would look like this:

    ### Function: Add Rating Custom Fields
    add_action('publish_post', 'add_ratings_fields');
    add_action('publish_page', 'add_ratings_fields');
    add_action('publish_shop_item', '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);
    	}
    }

    I'm afraid I can't comment on the way this affects the widget as I'm not using it, I was creating some custom templates with these features built in.

    But I hope that helps.

  22. nothin7
    Member
    Posted 6 months ago #

    Is there a way to modify this function so that it adds the post meta to all of my thousands of already published custom type posts? Or is there any chance of this being included in a future release of this plugin? I would love to be able to sort by highest rated while still keeping unrated items in the results. Thanks!

  23. KS Web Designer
    Member
    Posted 6 months ago #

    I thought this did just that... I am using it in a slightly different way now due to the way my own site has evolved but I thought at the time it did list all posts (including those with no ratings)

  24. nothin7
    Member
    Posted 6 months ago #

    From the code it would seem this adds the necessary fields when a new custom type post is published, but doesn't go back through all posts that were published before this function was instated.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.