Sort by custom field value

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

🙂

Leave a Comment