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