Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query - need to sort by a custom field. WordPress

  • SOLVED

This is the current query:
$loop = new WP_Query(array('post_type' => 'event', 'posts_per_page' => -1, 'order' => ASC));

These are some of my custom fields:
$event_price = $custom["event_price"][0];
$event_start_date = $custom["event_start_date"][0];
$event_end_date = $custom["event_end_date"][0];



What I want to be able to do is run the query but sort by my event_start_date ascending.

Answers (3)

2011-01-27

Jens Filipsson answers:

Something like this should work:

$loop = new WP_Query(array('post_type' => 'event', 'posts_per_page' => -1, 'meta_key' => 'event_start_date', 'orderby' => 'meta_value', 'order' => ASC));

2011-01-27

John Cotton answers:

Add meta_key= 'event_start_date' to your array.....


69developer comments:

So I add 'meta_key' => 'event_start_date' ?

Then what about the ordering by the event_start_date?


John Cotton comments:

orderby=meta_value_num&order=ASC

2011-01-27

Oleg Butuzov answers:

new WP_Query(
array('post_type' => 'event',
'posts_per_page' => -1,
'order' => ASC,
'orderby'=>'meta_value_num',
'meta_key'=>'event_price'
)
);


if price is int

else case use just 'orderby'=>'meta_value'