WP_Query search by date OR custom field - php

I'm looking for a way to filter wordpress WP_Queries by date or a custom field.
I'm attempting to pull out all posts published after a certain date or posts marked with a given meta_key.
So far I can see how to have an or clause for different date_query options or different meta_query options, but not between the two.
this is what I have tried so far with the meta_query filter and date_query filter
$args = array(
'category_name' => 'mcr-bulletin',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'ASC',
'relation' => 'OR',
array(
'date_query' => array(
'after'=>array(
'year' => $date->format('Y'),
'month' => $date->format('m'),
'day'=>$date->format('d')
)
),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'end_date',
'value' => $now->format("Ymd"),
'type'=>'NUMERIC',
'compare' => '>' ),
array(
'key' => 'repeat_post',
'value' => 1,
'type' => 'NUMERIC',
'compare'=> '=')
)
)
)
);
Update
Update with current work around. I can achieve the desired functionality using 2 wpdb queries and 2 corresponding loops through posts. However, if possible I want to combine the queries into a single argument, reducing db access and simplifying the code.
global $wpdb;
$message = "";
$message2 = "";
$now = new DateTime();
$date = new DateTime();
$date->setTimestamp( mktime( 0, 0, 0, date( "m" ), date( "d" ) - 7, date( "Y" ) ) );
$args = array(
'category_name' => 'mcr-bulletin',
'post_status' => 'publish',
'posts_per_page' => - 1,
'orderby' => 'date',
'order' => 'ASC',
'date_query' => array(
'after' => array(
'year' => $date->format( 'Y' ),
'month' => $date->format( 'm' ),
'day' => $date->format( 'd' )
)
)
);
$args2 = array(
'category_name' => 'mcr-bulletin',
'post_status' => 'publish',
'posts_per_page' => - 1,
'orderby' => 'date',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'end_date',
'value' => $now->format( "Ymd" ),
'type' => 'NUMERIC',
'compare' => '>' ),
array(
'key' => 'repeat_post',
'value' => 1,
'type' => 'NUMERIC',
'compare' => '='
)
),
'date_query' => array(
'before' => array(
'year' => $date->format( 'Y' ),
'month' => $date->format( 'm' ),
'day' => $date->format( 'd' )
)
)
);
$query = new WP_Query( $args );
$query2 = new WP_Query( $args2 );

Related

wc_order_query with meta_query not working when using relation

I am trying to use WC_Order_Query, to get all orders where a custom meta_key doesn't exist, is empty or equal to 0
I've tried like a lot of the stuff documented on this site, but nothing seems to work. It just returns all content, which is the opposite of what i'm trying to do.
This is what most people have recommended so far, but it doesn't seem to work as intended or maybe I am not seeing the issue here
$args = array(
'limit' => 9999,
'return' => 'ids',
'orderby' => 'date',
'order' => 'DESC',
'status' => 'processing',
'date_created' => '>='.$startdate,
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WC_Order_Query( $args );
$orders = $query->get_orders();
This ended up being the solution:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'post_status' => 'any',
'orderby' => 'the_date',
'order' => 'DESC',
'date_query' => array(
array(
'after' => $startdate . $starttime,
'inclusive' => true
)
),
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WP_Query( $args );
$query_posts = $query->get_posts();
Thanks a lot to Boris for the assistance.

Wordpress query based on custom date field and time field

I am using a WordPress Calendar plugin to show multiple events. These events start on different days on different times. The plugin works fine and shows all the the events.
But I wanna display just the future events. E.g., if an event starts at August 1st at 11am, the event should be hidden at the same time. I tried to solve my problem with this query:
add_filter('cmcal_calendar_posts_query', 'calendar_return_specific_events', 10, 2);
function calendar_return_specific_events($query_args, $calendar_id) {
if ($calendar_id == 110) {
$todays_date = current_time('d.m.Y');
$right_now = current_time('H:i');
$query_args = array(
'post_type' => 'product',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'start_date',
'compare' => '>=',
'value' => $todays_date,
),
'time_clause' => array(
'key' => 'start_time',
'compare' => '>=',
'value' => $right_now,
),
),
'orderby' => array(
'date_clause' => 'ASC',
'time_clause' => 'ASC',
)
);
}
return $query_args;
}
But unfortunately it doesn't work. What am I doing wrong?
Thank you for your help.
you should set key 'type' in your 'meta_query' array. The code would be something like this:
add_filter('cmcal_calendar_posts_query', 'calendar_return_specific_events', 10, 2);
function calendar_return_specific_events($query_args, $calendar_id) {
if ($calendar_id == 110) {
$todays_date = current_time('d.m.Y');
$right_now = current_time('H:i');
$query_args = array(
'post_type' => 'product',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>=',
'value' => $todays_date,
'type' => 'DATE' //set the format
),
array(
'key' => 'start_time',
'compare' => '>=',
'value' => $right_now,
'type' => 'DATE' //set the format
),
),
'orderby' => array(
'date_clause' => 'ASC',
'time_clause' => 'ASC',
)
);
}
return $query_args;
}

Wordpress ACF datepicker fields not working

I created a "vacations" custom-post-type in which there are two custom fields -> "startdate" and "enddate". The field types are both "Date Picker" obviously.
But for some reason I dont know, I'm not able to fetch these fields. It returns nothing or null.
In my functions.php I have this:
function get_vacation_dates(){
$startDate = get_field('vacation_start', false, false);
$endDate = get_field('vacation_end', false, false);
$start_date = new DateTime($startDate);
$end_date = new DateTime($endDate);
$args = array(
'post_type' => array('ferieperioder'),
'post_status' => array('publish'),
'posts_per_page' => -1,
'post__in' => $post->ID,
'meta_key' => array(
'key' => 'vacation_start',
'value' => $startDate
),
array(
'key' => 'vacation_end',
'value' => $endDate
)
);
$query = new WP_Query($args);
echo json_encode($query);
die();
}
add_action('wp_ajax_nopriv_get_vacation_dates', 'get_vacation_dates');
add_action('wp_ajax_get_vacation_dates','get_vacation_dates');
I don't know if this is correct at all, since I want to get each vacation_period (there can be multiple "vacation"-posts) returned as json for some AJAX-handling.
For example I want an json array like this:
[ {
"vacation_name" : "summer",
"vacation_start" : "01-07-2019",
"vacation_end" : "30-07-2019"
},
{
"vacation_name" : "winter",
"vacation_start" : "01-12-2019",
"vacation_end" : "14-12-2019"
},
]
or something like that...
How can I achieve that?
I think you should do it with meta_query like this:
$args = array(
'post_type' => array('ferieperioder'),
'post_status' => array('publish'),
'posts_per_page' => -1,
'post__in' => $post->ID,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'vacation_start',
'value' => $startDate,
'type' => 'DATE',
'compare' => '>='
),
array(
'key' => 'vacation_end',
'value' => $endDate,
'type' => 'DATE',
'compare' => '<='
)
)
);

wordpress new WP_Query between not working

How can I make it filter between the two integers?
This is my PHP
$filter = array(
'post_type' => 'request',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'first_posting_date',
'value' => array('20170826','20170829'), //already tried to remove the qoutes on numbers
'compare' => 'BETWEEN'
)
);
$posts = new WP_Query($filter);
print_r($posts);
The results of print_r it displaying the outside of the filtered of two integers. Where I have gone wrong?
Multiple meta values can be compared with BETWEEN with an array value:
'meta_query' => array(
array(
'key' => 'first_posting_date',
'value' => array('20170826','20170829'),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
You can see this in Codex
Try it:
//Date
$start = '2017-08-26';
$end = '2017-08-29';
$filter = array(
'post_type' => 'request',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'first_posting_date',
'meta_query' => array(
array(
'key' => 'first_posting_date',
'value' => array($start, $end),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
)
);
// Make the query
$posts = new WP_Query($filter);
print_r($posts);
The answer to my question is relation and put the array of meta_query to other array
so it would be like this
$filter = array(
'post_type' => 'request',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
"relation" => "AND",
array(
'key' => 'first_posting_date',
'value' => array('20170826','20170829'),
'compare' => 'BETWEEN'
)
)
);
$posts = new WP_Query($filter);
print_r($posts);

WordPress Meta-Query between 2 dates

I have currently a small problem understanding with meta-queries by WordPress. Initial situation:
A Custom Post Type with 2 meta-Fields (offer_start-date, offer_end-date) The CPT is intended as an offer, which should be displayed in the specified time period (between start-date and end-date) . The date here is formatted in german format DD.MM.YYYY. For that I use currently following query:
$args = array(
'post_type' => 'offer',
'posts_per_page' => -1,
'post_status' => 'publish',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'offer_start-date',
'value' => date( 'd.m.Y', time() ),
'type' => 'numeric',
'compare' => '<='
),
array(
'key' => 'offer_end-date',
'value' => date( 'd.m.Y', time() ),
'type' => 'numeric',
'compare' => '>='
)
)
);
new WP_Query( $args );
Unfortunately, the query does not yield reliable results. I can not even say 100% why. On some days all offers appear, on other days there are no offers.
I have also tried to find out the cause of the problem in the Codex, but it seems I am a strong blockhead.
If you need tu use between just use like that:
'meta_query' => array(
array(
'key' => 'event_date',
'value' => array(date('d/m/Y'), date('d/m/Y', strtotime('28 days'))),
'compare' => 'BETWEEN',
'type' => 'DATE'
),
)
This below solution may be handy to someone
$args = array(
'cat' => $cat_ID,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date_from',
'value' => date("Y-m-d H:i:s"),
'compare' => '<=',
'type' => 'DATE'
),
array(
'key' => 'date_to',
'value' => date("Y-m-d H:i:s"),
'compare' => '>=',
'type' => 'DATE'
)
),
'orderby' => 'date',
'order' => 'DESC'
);

Categories