WordPress.org

Ready to get started?Download WordPress

Forums

wp_query orderby meta_value - not expected results (6 posts)

  1. cwz001
    Member
    Posted 3 years ago #

    Hi I'm running this query

    $args=array(
    '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!

    Thanks

  2. MichaelH
    Member
    Posted 3 years ago #

    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: http://wordpress.org/support/topic/286391?replies=12#post-1284231

  3. cwz001
    Member
    Posted 3 years ago #

    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?

    Thanks

  4. MichaelH
    Member
    Posted 3 years ago #

    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:
    http://wordpress.org/support/topic/397434?replies=2

  5. cwz001
    Member
    Posted 3 years ago #

    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!

  6. MichaelH
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic