Wordpress ACF datepicker fields not working - php

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' => '<='
)
)
);

Related

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 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);

Visual composer custom query - excluding meta_key

I have a custom query that I would like some help converting to visual composer's custom query. Basically, I would like to exclude all posts from displaying in the post grid that have the meta_key: _is_featured_posts and its value as yes.
// WP_Query arguments
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
// The Query
$query = new WP_Query( $args );
Any help would be appreciated.
Thanks
There is an alternative solution, it is not recommended but as NOT EXISTS is not working so you can use the following code. I also check the solution given here, but it's not working either.
//to hold the post id which has _is_ns_featured_post => 'yes'
$exclude_id = array();
$args_exclude = array(
'post_type' => array('post'),
'post_status' => array('publish'),
'posts_per_page' => '-1',
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
),
),
);
$exclude_posts = new WP_Query($args_exclude);
if (!empty($exclude_posts->posts))
{
foreach ($exclude_posts->posts as $post)
{
$exclude_id[] = $post->ID;
}
}
$args = array(
'post_type' => array('post'),
'post_status' => array('publish'),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => $exclude_id //exclude post_id which has _is_ns_featured_post => 'yes'
);
// The Query
$query = new WP_Query($args);
foreach ($query->posts as $post)
{
print_r($post);
}
Hope this helps!
See: visual composer wordpress query for post grid
Try this:
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
echo http_build_query($args);
// Result:
post_type%5B0%5D=post&post_status%5B0%5D=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query%5Brelation%5D=AND&meta_query%5B0%5D%5Bkey%5D=_is_ns_featured_post&meta_query%5B0%5D%5Bvalue%5D=yes&meta_query%5B0%5D%5Bcompare%5D=NOT+EXISTS
http://sandbox.onlinephpfunctions.com/code/5c2bc6ddd37a02fc8facf4f227176e262854b92e
I would recommend to avoid use array('post') in case if only one post type, so just use post_type=post&post_status=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query[relation]=and&meta_query[0][key]=_is_ns_featured_post&meta_query[0][value]=yes&meta_query[0][compare]=NOT EXISTS
P.S.
possibly %5B and %5D you will need to convert back to [ and ] via echo urldecode(http_build_query($args));

WP_Query search by date OR custom field

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 );

Use meta to match date in WP_Query

I have some posts with a custom filed named "event_end_date". I am using Advanced custom fields for that. How can I get the posts which have end_date older than today? I was trying with this, but could not get through.
$args = array(
'cat' => '6, -33',
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'event_end_date',
'value' => date('y-m-d'),
'type' => 'DATE',
'compare' => '>'
)
)
);
//get results
$the_query = new WP_Query( $args );
Here I wanted those posts which have category '6' and does not have category '33'. In the backend, the field - 'event_end_date' has these:
Field Type: Date Picker
Display format: 10/11/2014
Return format: 20141110
I think if you were too look more closely at the data being stored in that actual custom/meta field, you'd find that it's stored as a unix timestamp, not in the return or display format, and because of that, your problem is that your format's don't match up.
You can account for that difference by using the U code for unix timestamp with date('U') instead of date('y-m-d').
In that case your code would look like this:
$args = array(
'cat' => '6,-33',
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'event_end_date',
'value' => date('U'),
'type' => 'DATE',
'compare' => '>'
)
)
);
//get results
$the_query = new WP_Query( $args );
Using date('Y-m-d', time()) solved the problem for me.
$args = array(
'category__and' => array(6),
'category__not_in' => array(33),
'numberposts' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'event_end_date',
'value' => date('Y-m-d', time()),
'type' => 'DATE',
'compare' => '<'
)
)
);

Categories