WordPress.org

Support

Support » Plugins and Hacks » [Resolved] URL Query string to order posts by highest score missing posts

[Resolved] URL Query string to order posts by highest score missing posts

Viewing 15 replies - 1 through 15 (of 23 total)
  • Plugin Author Lester Chan

    @gamerz

    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…?

    Plugin Author Lester Chan

    @gamerz

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

    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.

    Plugin Author Lester Chan

    @gamerz

    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?

    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).

    Plugin Author Lester Chan

    @gamerz

    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.

    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)

    Plugin Author Lester Chan

    @gamerz

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

    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).

    Plugin Author Lester Chan

    @gamerz

    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.

    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

    Plugin Author Lester Chan

    @gamerz

    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>.

    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?

    Plugin Author Lester Chan

    @gamerz

    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()?

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘[Resolved] URL Query string to order posts by highest score missing posts’ is closed to new replies.