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 ); ?>
When I put var_dump($query2->request) after this, I get the following string:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id)
WHERE 1=1 AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
AND (
(wp_postmeta.meta_key = 'date2' AND CAST(wp_postmeta.meta_value AS CHAR) BETWEEN '2013-05-21' AND '2013-05-28')
OR (mt1.meta_key = 'date3' AND CAST(mt1.meta_value AS CHAR) BETWEEN '2013-05-21' AND '2013-05-28')
OR (mt2.meta_key = 'date1' AND CAST(mt2.meta_value AS CHAR) BETWEEN '2013-05-21' AND '2013-05-28')
)
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC LIMIT 0, 10
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 end up with all of the events posted, and not sorted either. 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 ); ?>
When I put var_dump($query2->request) after this I get:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id)
INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id)
WHERE 1=1
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
AND (wp_postmeta.meta_key = 'date1'
OR (mt1.meta_key = 'date2' AND CAST(mt1.meta_value AS CHAR) BETWEEN '2013-05-21' AND '2013-05-28')
OR (mt2.meta_key = 'date3' AND CAST(mt2.meta_value AS CHAR) BETWEEN '2013-05-21' AND '2013-05-28')
OR (mt3.meta_key = 'date1' AND CAST(mt3.meta_value AS CHAR) BETWEEN '2013-05-21' AND '2013-05-28')
)
GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value+0 ASC LIMIT 0, 10
var_dump($query2->request);
right below thenew WP_Query
line and post the output, please.var_dump
I asked for.