I have a page where I only want to show posts from a specific category. At the same time, I want the posts to be sorted by a custom field value in my posts:
query_posts('category_name=' . str_replace(" ", "", $store_name) . '&meta_key=wpfp_favorites&orderby=meta_value_num');
The sorting is not working however. The posts are not sorted by the custom field value. Any ideas why? What do I need to make it work?
Added:
In a similar case, it is working perfectly:
query_posts('tag=' . $pagetitle . '&meta_key=wpfp_favorites&orderby=meta_value_num');
Have you tried hard-coding the category parameter for testing, and additionally trying different methods to set the query parameters..
Eg.
Where N would be a numerical ID number for the category.
Logically the query class should accept both a category and a meta key, whilst being able to order by meta key, i can’t see any conditional logic that would prevent these two parameters working alongside one another..
Maybe an additional test would be to set the
meta_value
parameter to and see if this has an effect, you could use something like this to avoid the need to match a particular value.. (which would match any post with that custom field key where the value isn’t an empty string)RE: Get category ID…. get_cat_ID(‘nameofcat’) or alternatively take a peek at the edit URL for the category in the administration area it should contain the category ID..
🙂
Dump generated SQL request. See if everything looks fine, try to run it directly on database.