Support » Fixing WordPress » wp_query orderby meta_value – not expected results

  • Hi I’m running this query

    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘meta_key’ => array(‘pgopts’, ‘orderbynum’),
    ‘cat’ => $cats,
    ‘paged’ => $paged,
    ‘orderby’ => ‘meta_value’,
    ‘order’ => ‘DESC’,
    ‘posts_per_page’ => -1,
    ‘caller_get_posts’=> 1

    It seemed to be working as I put 0 in one of the posts and it appeared at the beginning, but as soon as I increased the numbers I received random results. Even putting 01 02 etc, 02 would appear before 01??

    Any help would be greatly appreciated!


Viewing 5 replies - 1 through 5 (of 5 total)
  • This can’t be an array, only one value allowed

    'meta_key' => array('pgopts', 'orderbynum'),
    'meta_key' => 'pgopts',

    Numeric values may not sort correctly until version 3.0 but there is this thread that may apply:

    Hi Michael,

    Unfortunately pgopts is an array. Is there no way I can access the orderbynum value or do I need to regsiter the value a different way?


    You’ll have to do the query without the meta_key argument, then in your loop, get the custom field value and do your test there.

    Similar to this:

    I’m using something similar to that anyway (see below), It might be just I’m tired but how can I use the “orderbynum” values to re-order output of the posts?

    while ($featured_query->have_posts()) : $featured_query->the_post();

    /****************** Get custom field data ******************/

    $pdata = get_post_meta( $post->ID, 'pgopts', true );

    Thanks again for your time Michael!

    One way would be to extract custom field value, put it in an array and sort that.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_query orderby meta_value – not expected results’ is closed to new replies.