sorting – wp_query using meta_value_num is not working

i’ve seen some questions on this but the answers were not clear and definite.

i have a custom query, each of the posts in that custom post type have a custom field named “my_order” with a numeric value. whatever i tried i cannot get them to sort by that value. any clue ?

   <?php $args = array(
        'post_type' => 'portfolio', 
        'orderby' => 'meta_value',
        'meta_value_num' => 'my_order' 
        'order' => 'ASC', 
        'meta_query' => array(
            array(
                'key' => 'my_order'
            )
        )
    );
    $loop = new WP_Query( $args );

Related posts

Leave a Reply

2 comments

  1. Would this work for you?

       <?php $args = array(
        'post_type' => 'portfolio', 
        'orderby' => 'meta_value',
        'meta_value_num' => 'my_order' 
        'order' => 'ASC', 
        'meta_key' => 'my_order'
        )
    );
    $loop = new WP_Query( $args );
    
  2. silly me,

    it should be ‘orderby’ => ‘meta_value_num’
    and ‘meta_key’ => ‘my_order’

    $args = array(
        'post_type' => 'portfolio', 
        'orderby' => 'meta_value_num',
        'meta_key' => 'my_order',
        'order' => 'ASC'
    
    );
    $loop = new WP_Query( $args );
    

    Thanks…