0
votes

I will try to make this as short and clear as possible.

What is my goal?

My goal is to sort posts in my category page by meta key. I have a category named "Rating" and each post in that category has a meta key named "Rating" - in it is a numerical value.

So, for example, I have "Restaurant A" and "Restaurant B" in category "Rating". One has meta key "Rating" value of 56, the other one 40. I would like to show them from the highest rating to the lowest rating (not alphabetically, chronologically or any other way.)

What have I tried?

I have tried adding this directly into my category.php file:

<?php global $query_string;
$posts = query_posts('posts_per_page=5&cat=rating&meta_key=rating&order=ASC'); 
?>

before this part, which was already in category.php file:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php fotomag_post_layout(); ?>
<?php endwhile; ?>
<?php endif; ?>

and ended the code with this part:

<?php wp_reset_query(); ?>

How did I fail?

It partially works. It does show, for example, 5 posts and if I delete meta key from some of the posts in the "Rating" category it doesn't show them here (which is correct - because the query is to show posts in Rating category which have a Rating metafield). But it does not order it by ascending or descending means.

What am I doing wrong? Should I put any kind of filter or action hook or something in my functions file? Is something messing up with my theme? (I have no other plugins installed by the way)

Additional files:

My functions.php file (with the add_action hook mentioned in the answer below) - https://pastebin.com/WYyRknD6

My category.php file - https://pastebin.com/nWFgL1bV

My post layout file (this is the file that gets called in the category.php file - it maybe is or isn't relevant) - https://pastebin.com/3M6mrQVY

You can check the live version of the page here - https://preprod.pojej.me/category/rating/ - the number shown on the post is the number value in the "Rating" meta key field (minus the % symbol).

1

1 Answers

0
votes

Instead of that, you'll want to use the pre_get_posts filter to modify the main query and have it return posts ordered by your meta field. This way, if you want to extend this functionality to other categories (or taxonomies) later you'll only need to change the code in one place.

For example:

function wp87615486_custom_sort_rating_category( $query ) {

    /**
     * Modify the 'rating' category main query
     * to have it sort posts by meta rating value.
     */
    if (
        is_category( 'rating' ) // we're currently viewing the 'rating' category
        && !is_admin() // and we're not in the admin
        && $query->is_main_query() // and this is WP's main query
    ) {

        $posts_per_page = get_option( 'posts_per_page' );

        $query->set( 'posts_per_page', $posts_per_page );
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'meta_key', '_lets_review_final_score' );
        $query->set( 'order', 'DESC' ); // ASC will return entries with the lowest rating, DESC the ones with the higher rating

    }

}
add_action( 'pre_get_posts', 'wp87615486_custom_sort_rating_category' );

Then you can use the regular loop in category.php.