Reputation: 51
I am writing a code to query posts meta value based on other meta value compare date, below is my code, it query all posts ignoring the date condition:
i have tried to use 'LEFT JOIN" sentence with no success
$querystr = " SELECT DISTINCT m2.meta_value as 'appdata' FROM $wpdb->posts INNER JOIN $wpdb->postmeta m1 ON ($wpdb->posts.ID = m1.post_id AND m1.meta_key LIKE 'repeater_%_date' AND m1.meta_value > NOW()) INNER JOIN $wpdb->postmeta m2 ON ($wpdb->posts.ID = m2.post_id AND m2.meta_key LIKE 'repeater_%_appdata' AND m2.meta_value != '') WHERE $wpdb->posts.ID = m1.post_id "; $pageposts = $wpdb->get_results($querystr, ARRAY_A);
The problem is this code query the old and new posts which has the date meta key
Note: repeater_%_date is ACF date field storing the date as Ymd (20190708)
Any solution please?
Upvotes: 0
Views: 1314
Reputation: 301
$get_featured_args3 = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page'=>-1,
'meta_query' => array(
'relation' ->'AND',
array(
'key' => 'key1',
'value' => 1,
'compare' => '=',
),
array(
'key' => 'key2',
'value' => 1,
'compare' => '=',
),
),
'date_query' => array(
array(
'year' => date('Y'),
),
),
);
$get_featured_results3 = new WP_Query( $get_featured_args3 );
$posts = $get_featured_results3->posts;
foreach($posts as $post){
}
Upvotes: 0