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.

In my plugin, I use these instructions to obtain a list of posts:

$args = array(
    'numberposts'     => -1,
    'offset'          => 0,
    'meta_query' => array(
        array(
            'key' => 'metadata1',
            'value' => '80',
            'compare' => '<=',
            'type' => 'UNSIGNED'
        ),
        array(
            'key' => 'metadata2',
            'value' => '4.6',
            'compare' => '<=',
            'type' => 'DECIMAL'
        ),
    ),
    'post_type'       => 'post',
    'post_status'     => 'publish',
    'suppress_filters' => true );

$posts = get_posts($args);

foreach ($posts as $post) {
    // some work on $post
}

How can I order these posts by metadata1 ascending or by metadata2 ascending? (Note that metadata1 is an unsigned integer number, and metadata2 is a decimal number.)

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

Try meta_value_num

'meta_key' => 'metadata1',
'orderby' => 'meta_value_num'

Add these 2 parameters to $args. This does the sorting considering metadata1 as numeric

share|improve this answer
add comment

Also, if you can't get this to work with get_posts, I'd recommend switching to WP_Query.

As Mridul said above, to sort based on numbers, you need to specify the 'meta_value_num' to 'orderby' parameter. However, I'm not certain that you can do this with a standard get_posts() call.

share|improve this answer
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.