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'
));
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'
));