querying on custom meta fields and sorting them by custom meta
-
I have event posts with multiple custom date and time fields.
To pull all the events happening this week, I am querying on the different date fields.
Here is my code:
$today=date(“Y-m-d”);
$nextweek= date(“Y-m-d”, strtotime(“+7 days”));$args2 = array(
‘post_type’ => ‘post’,
‘meta_query’ => array(
‘relation’ => ‘OR’,array(
‘key’ => ‘date2’,
‘value’ => array( $today, $nextweek ),
‘compare’ => ‘BETWEEN’
),
array(
‘key’ => ‘date3’,
‘value’ => array( $today, $nextweek ),
‘compare’ => ‘BETWEEN’
),
array(
‘key’ => ‘date1’,
‘value’ => array( $today, $nextweek ),
‘compare’ => ‘BETWEEN’)
)
);
$query2 = new WP_Query( $args2 ); ?>This is pulling the correct posts, but now I want to sort the results on the date1 field. I tried this and it isn’t working. I’ve also tried the ‘meta_value’ for orderby without luck. The values in the date1 field are in date format YYYY-MM-DD
Any help would be appreciated.$args2 = array(
‘order’ => ‘ASC’,
‘meta_key’ => ‘date1’,
‘orderby’ => ‘meta_value_num’,
‘post_type’ => ‘post’,
‘meta_query’ => array(
‘relation’ => ‘OR’,array(
‘key’ => ‘date2’,
‘value’ => array( $today, $nextweek ),
‘compare’ => ‘BETWEEN’
),
array(
‘key’ => ‘date3’,
‘value’ => array( $today, $nextweek ),
‘compare’ => ‘BETWEEN’
),
array(
‘key’ => ‘date1’,
‘value’ => array( $today, $nextweek ),
‘compare’ => ‘BETWEEN’,)
));
$query2 = new WP_Query( $args2 ); ?>
- The topic ‘querying on custom meta fields and sorting them by custom meta’ is closed to new replies.