Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
share|improve this question
    
When you say that "The posts are not sorted by the custom field value", what exactly do you see? When sorting by custom field, WP treats custom field values as strings.. so if your custom field is a numeric or date value, you may get unexpected results –  FOR Dec 1 '10 at 18:07
    
The wpfp_favorites field is allways a numeric value. I´m using meta_value_num, hence it expects a numeric value. Shouldn´t be a problem then. –  user1635 Dec 1 '10 at 18:34

2 Answers 2

Have you tried hard-coding the category parameter for testing, and additionally trying different methods to set the query parameters..

Eg.

query_posts( array( 'category_name' => 'myname', 'meta_ke' => 'wpfp_favorites', 'orderby' => 'meta_value_num' ) );
query_posts( array( 'cat' => N, 'meta_key' => 'wpfp_favorites', 'orderby' => 'meta_value_num' ) );
query_posts( array( 'cat__in' => array( N ), 'meta_key' => 'wpfp_favorites', 'orderby' => 'meta_value_num' ) );

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)

query_posts( array( 
    'cat' => N, 
    'meta_key' => 'wpfp_favorites', 
    'meta_value' => '', 
    'meta_compare' => '!=', 
    'orderby' => 'meta_value_num' 
) );

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

:)

share|improve this answer
1  
+1 for hardcoding category for starters. Also must note that argument is called category_name but what it actually accepts is category slug rather than name. I stepped on this mine in the past. :) –  Rarst Dec 1 '10 at 6:14
    
That's a good catch, i do specifically remember category_name expecting a slug despite the name also, now that you've mentioned it. –  t31os Dec 1 '10 at 11:12
    
How would I obtain the category ID if i only know the category name? –  user1635 Dec 1 '10 at 18:16
    
None of these variations has any impact. The posts are sill being sorted the default way (by time posted). Any other ideas? –  user1635 Dec 1 '10 at 18:20
    
All but the "meta_value" orderby parameters seems to work –  user1635 Dec 1 '10 at 18:24

Dump generated SQL request. See if everything looks fine, try to run it directly on database.

add_filter( 'posts_request', 'dump_request' );

function dump_request( $input ) {

    var_dump($input);

    return $input;
}
share|improve this answer
    
Could you please explain me what this does? Thanks! –  user1635 Dec 1 '10 at 21:30
    
This should output complete SQL request that is generated and executed when query_posts() runs. You can go over it (and/or post here) to check for errors and use tool like PHPMyAdmin to run request directly on database and see if it works like it should. –  Rarst Dec 1 '10 at 21:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.