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.

i have a custom query, and I have a meta key that contains a sequence number: 1.1, 1.2, 2.1, 2.2... 11.1, etc

the problem occurs when the numerical sequence gets in to double digits, ie. 5.11, 5.12, 5.21

these appear as such

5.1, 5.11, 5.12, 5.2, 5.21 etc instead of 5.1, 5.2, 5.3... 5.11, 5.12 etc

how can I make them sort properly!?

$loop = new WP_Query(array(
    'factsheet_category' => $term->slug,
    'orderby' => 'meta_value_num', 
    'meta_key' => 'factsheet_id', 
    'order' => 'ASC'
));
share|improve this question
add comment

1 Answer

You want natural order sorting,

Example;

$val = array(5.1, 5.11, 5.12, 5.2, 5.21);
natsort($val);
foreach ($val as $key => $val) {
    echo $val."<br>";
}

Produces:

5.1
5.2
5.11
5.12
5.21

Where as;

$val = array(5.1, 5.11, 5.12, 5.2, 5.21);
asort($val);
foreach ($val as $key => $val) {
    echo $val."<br>";
}

Produces:

5.1
5.11
5.12
5.2
5.21

Solution

$loop = new WP_Query(array(
    'factsheet_category' => $term->slug,
    'orderby' => 'meta_value meta_value_num', //addition of meta_value before meta_value_num
    'meta_key' => 'factsheet_id', 
    'order' => 'ASC'
));
share|improve this answer
    
sounds logical, but it doesn't make a difference - they are still sorted as per your second example :( –  Elle Billias Oct 5 '12 at 10:33
    
+1 for the answer... @ElleBillias It does. Just use a natsort() call to the result. –  kaiser Oct 5 '12 at 12:11
    
How would I go about that? I adde the extra orderby value but it didn't change. How can I add natsort() to it? –  Elle Billias Oct 5 '12 at 23:03
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.