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.

Aa you know, as of WP3.0 there are options for custom advanced queries, which is great. As of this, some query parameters of custom fields like meta_key, meta_value were deprecated for the new meta_query parameter (see here)

I try to have a pretty simple query with the new syntax, query posts by a certain post_type (services) that contains a specified meta_key (order_in_archive)- this is going well as expected. But - I want to orderby the query by the meta_value, and with no success.

This is my query -

   query_posts(
    array(  'post_type' => 'services',
        'order' => 'ASC',
        'orderby' => 'meta_value',
        'meta_query' => array(
            array('key' => 'order_in_archive'))
    )
);

I tried orderby also by meta_value_numeric and meta_value, but in any case the results are being ordered by the publication date (as regular posts do). Anyone know how can this be done?

Thanks

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

You can define the meta key for orderby parameter using the old method (I tested on WP 3.1.1)...

query_posts(
    array(  'post_type' => 'services',
            'order'     => 'ASC',
            'meta_key' => 'some_key',
            'orderby'   => 'meta_value', //or 'meta_value_num'
            'meta_query' => array(
                                array('key' => 'order_in_archive',
                                      'value' => 'some_value'
                                )
                            )
    )
);
share|improve this answer
add comment

I found this to work quite well.

<?php 
query_posts(
array(  'posts_per_page' => '-1',
        'post_type' => 'services',
        'order'     => 'DESC',
        'meta_key' => '_order',
        'orderby'   => 'meta_value_num', //or 'meta_value_num'
      )
);
share|improve this answer
    
The question is about the new-ish meta_query parameter and getting an orderby to work with it, and not about the older but still functional meta_key/meta_value parameters. Also, let's not encourage the use of query_posts. –  s_ha_dum Dec 28 '12 at 19:55
add comment

The WP Codex is actually confusing when addressing this problem.

You do not actually need the meta_query param to use the orderby, instead it uses the meta_key param, which although by WP Codex is deprecated is has been established here: How do you use orderby with meta_query in Wordpress 3.1? that orderyby still needs the meta_key.

so it should be

query_posts( array(
  'post_type' => 'services',
  'order' => 'ASC',
  'orderby' => 'meta_value',
  'meta_key' => 'order_in_archive'
) )
share|improve this answer
2  
Well, yes, this is the old method for doing this query and it appears to be working for this specified one. Anyway, for more complex queries, it won't work. I found this is a known issue that is being taken care of, details can be found in trac tickets #15031 and #17065 –  Maor Barazany Apr 24 '11 at 0:14
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.