Posts just won't order by custom field
I’ve hunted around for an answer to this for two days now, and just can’t seem to get this code to work. I’m trying to order results in ascending order by a custom field called ‘price’. I understand there’s an issue ordering by numbers rather than strings, but I have tried every combination of meta_value, meta_value_num, with and without quotes, and am getting absolutely nothing. The thing I really don’t understand is I only get results of any description when I set the ‘greater than >’ to be ‘less than <‘ zero, which is palpable nonsense. The results I get then bring back an unordered list of the posts I’m after.
I can’t give a link to the site because it’s running off WAMP, but hopefully some merciful soul will see something in the code:
global $wpdb; $querystr = " SELECT * FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta , $wpdb->postmeta wpostmeta2, $wpdb->postmeta wpostmeta3 WHERE wposts.ID = wpostmeta.post_id AND wposts.ID = wpostmeta2.post_id AND wposts.ID = wpostmeta3.post_id AND wpostmeta.meta_key = 'closed' AND wpostmeta.meta_value = '0' AND wpostmeta2.meta_key = 'pay_online' AND wpostmeta2.meta_value = '1' AND wpostmeta3.meta_key = 'price' AND wpostmeta3.meta_value_num > '0' AND wposts.post_status = 'publish' AND wposts.post_type = 'auction' ORDER BY wpostmeta3.meta_value_num ASC"; $pageposts = $wpdb->get_results($querystr, OBJECT); if ($pageposts): global $post; foreach ($pageposts as $post): setup_postdata($post); widget_market(); endforeach; else : ?> <div class="padd100"><p class="center"><?php _e("Be the first to list here"); ?>.</div>
- The topic ‘Posts just won't order by custom field’ is closed to new replies.