35

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

35

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'
                                )
                            )
    )
);
14

This issue in general is cleared up in WordPress 4.2 by using named queries. e.g.

$args = array(
  'post_type' => 'services',
  'orderby'   => 'order_clause',
  'meta_query' => array(
       'order_clause' => array(
            'key' => 'order_in_archive',
            'value' => 'some_value',
            'type' => 'NUMERIC' // unless the field is not a number
)));

For me, I wanted to order by a numeric field and I had to use 'type' => 'NUMERIC' inside the meta query.

  • 1
    Awesome find on the named queries! – Kaji Jul 30 '17 at 5:15
  • Yep, named queries answered the question for me, too. – Dalton Jul 31 '17 at 23:18
  • Excellent. I'm not sure the significance of 'value' in that order_clause because it's not required as it will order it by the highest to lowest value of 'order_in_archive'. – Andrew Schultz Dec 7 '18 at 3:34
10

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'
) )
  • 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
2

It´s easy:

Here is my code:

query_posts(array( 
        'post_type' => 'directors',
        'posts_per_page' => -1,
        'order'    => 'ASC',
        'orderby'  => 'director_weight',
        'meta_key' => 'director_weight'
) );

The main detail is: include meta_key, my code didn´t order unless meta_key is included, and that's all:

Here is the full code of a list of directors pictures order by director_weight:

<?php 
    query_posts(array( 
        'post_type' => 'directors',
        'posts_per_page' => -1,
        'order'    => 'ASC',
        'orderby'  => 'director_weight',
        'meta_key' => 'director_weight'
    ) );
    while (have_posts()) : the_post();
?>  
    <li <?php echo get_field('director_weight') ?>>
        <img src="<?php echo get_field('director_imagen') ?>">
    </li>
<?php
    endwhile;
    wp_reset_query();
?>

order_by custom fiel wordpress

1

Don't use query_posts.

$meta_query = new WP_Meta_Query( $meta_query_args );

$meta_query_args = array(
   'post_type' => 'services',
    'order'     => 'ASC',
    'meta_key' => 'your_key',
    'orderby'   => 'meta_value', //or 'meta_value_num'
    'meta_query' => array(
                    array('key' => 'order_in_archive',
                        'value' => 'some_value'
)));

Use the WP_Meta_Query parameters

0

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'
      )
);
  • 3
    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
  • This answer contains multiple bad practices: using -1, passing it as a string, using query_posts. It should be removed. – Ihor Vorotnov Mar 1 at 9:18
0

I had a set of custom event dates I needed to sort by date descending. Since my custom event date was stored in my wp_postmeta table, and was typically different to the post_date or modified dates, this worked for me:

  $args = array(
    'post_type' => 'events', // my post type - yours can be 'posts'
    'post_status' => 'publish', // only get posts with this status
    'orderby' => 'meta_value', // orderby the meta_value of the following meta_key
    'meta_key' => 'my_custom_eventdate', // the custom meta_key name
    'order'=> 'DESC' // sort descending
  );

  $posts = new WP_Query($args);

You can then loop through $posts like so:

foreach($posts->posts as $p){

    $post_id = $p->ID;
    // and so on ...

    // # example of how I retrieve my event date
    $event = get_post_meta($post_id, 'my_custom_eventdate', true);
}
0

This work for me,

    $args = array(
        'post_type' => 'services',
        'order' => 'ASC',
        'orderby'   => 'order_clause',   
        'meta_query' => array(
        'order_clause' => array(
        'key' => 'order_in_archive'
    )));

Only needed to provide the key for the order_clause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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