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' => '<'
)
)
);
Related
I'm looking for there to count all the posts that are there in future date to the current one
Query:
<?php
$mostra_data_corrente = date('d-m-Y');
$query = new WP_Query( array(
'post_type' => get_option('customer_postquery'),
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'metakey_AMC_data',
)
),
'date_query' => array(
'after' => $mostra_data_corrente,
),
'tax_query' => array(
array(
'taxonomy' => 'categoria',
'field' => 'slug',
'terms' => $queried_object,
)
) ) ) ;
$conta_risultati = $query->found_posts;
echo $conta_risultati;
?>
Where:
get_option('customer_postquery'): dynamically retrieves all the custom post types that have been created
metakey_AMC_data: is the meta key where the date of the event (post) is enclosed within the meta_value
$queried_object: dynamically retrieves the taxonomy of posts based on the page we are on, thus filtering the posts based on their taxonomy
So my intent is to count all the posts that are there that have a future date to the current one
"It does not count perfectly how many posts exist at a future date compared to the current one"
edit code:
<?php
$mostra_data_corrente = date('d-m-Y');
$query = new WP_Query( array(
'post_type' => get_option('customer_postquery'),
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'metakey_AMC_data',
'compare' => '>',
)
),
'date_query' => array(
'after' => $mostra_data_corrente,
),
'tax_query' => array(
array(
'taxonomy' => 'categoria',
'field' => 'slug',
'terms' => $queried_object,
)
)
) ) ;
$conta_risultati = $query->found_posts;
echo $conta_risultati;
result:3 , but it's not the truth because i have 7 result after current date for this taxonomy
May be you need to query them like :
$currentPostDate = 'get your post date here';
$args = array(
'post_type' => 'post',
'meta_key' => 'the_date',
'meta_query' => array(
array(
'key' => 'the_date',
'value' => $today,
'compare' => '>='
)
),
);
$your_custom_query = new WP_Query($args);
$postcount = count($your_custom_query);
Think to make it as function in your functions.php file.
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' => '<='
)
)
);
Problem
I am looping through custom post types (Advanced Custom Fields) in Wordpress. I only want to display events with start_date equal to $newdate variable, defined in the beginning.
The start_date is in the format YYYY-MM-DD HH:mm (the same as $newdate). $newdate is set to the beginning of the day so I wouldn't exclude events with different hours in the day and compare is set to greater than (just to test the query).
However I am not getting any results.
<?php
$newdate = date('Y-m-d 00:00');
//<-- Start the Loop. -->!
$args = array(
'post_type' => 'epsa_events',
'posts_per_page' => 5,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array (
array(
'key' => 'start_time',
'value' => $newdate,
'compare' => '>=',
'type' => 'datetime'
)
)
);
$loop = new WP_Query( $args );
Try this query:-
'meta_key' => 'event-start-date',
'meta_query' => array (
array(
'key' => 'start_time',
'value' => date('Ymd',strtotime($newdate)),
'compare' => '>=',
'type' => 'date'
)
)
I guess you have some small mistakes here.
First, if you use 'orderby' => 'meta_value' you must add a 'meta_key' => KEYVALUE to your $args where KEYVALUE is the name of the custom field you want to use for sorting. Check WP_Query documentation on Order & Orderby Parameters.
Second, assuming your start_date field type is Date Time Picker, if you want to get all events that already started you're using the wrong comparison operator. So assuming you also want to sort by date, your $args code should be:
$args = array(
'post_type' => 'epsa_events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'start_time',
'meta_type' => 'DATETIME'
'meta_query' => array (
array(
'key' => 'start_time',
'compare' => '<=',
'value' => $newdate,
'type' => 'DATETIME'
)
)
);
Take a look at Date Time Picker documentation too.
I hope this helps!
My current query looks like this.
$tag_id = 276;
$tag = get_tag($tag_id);
$today = date( "Y-m-d" );
$args = array(
'post_type' => array(
'post',
'review',
'cardguide'
),
'taxonomy' => 'post_tag',
'tag_id' => $tag_id,
'posts_per_page'=> - 1,
'orderby' => 'id',
'order' => 'DESC',
'meta_key' => 'event_end_date',
'meta_query' => array(
'relation'=> 'AND',
array(
'key' => 'event_start_date',
'value' => $today,
'compare'=> '<=',
'type' => 'DATE'
),
array(
'key' => 'event_end_date',
'value' => $today,
'compare'=> '>=',
'type' => 'DATE'
)
)
);
$my_query = new Wp_query( $args );
I want to include a single post (can use post id) into this query. The post may not obey each of the rules I defined inside $args. And also would like to have it always on top of the list (featured item).
If you are looking to get the same post by id for this query every time, then just do a separate get_post() call like:
$featured_item = get_post( 123 ); // replace 123 with your id
This will prevent you from having to worry about "obeying each of the rules" you've defined above. Plus, if you always want it "on top", then you can pump out the values of $featured_item before you run your query loop.
I am trying to query the database and ordering the result by the date and then by time. But with the query below it only orders by the start time. When I remove the start time it orders the result by date.
So far I have tried changing the order and multiple solutions throughout the web but nothing works.
$args = array(
'numberposts' => -1,
'post_type' => 'reservation',
'cat' => $categorie,
'meta_key' => 'start_time',
'orderby' => array( 'start_date' => 'ASC', 'meta_value_num' => 'ASC' ),
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>=',
'value' => $date,
),
array(
'key' => 'reservation_status',
'compare' => '<',
'value' => 3,
)
)
);
// query
$the_query = new WP_Query( $args );
Please help.
Thanks
How about something like this:
order => 'start_date start_time',
orderby => 'ASC',