Support Forum
Hi,
I have an Post type for Events, and some custom fields.
Custom field for START DATE and END DATE.
The Start date should be filled, and i have set an toggle for "multi-day" event, so if it say YES, the END DATE field also shows up.
Right now it hide events based on the start date.
But i want to hide events if there is an END DATE - so it should look for this first, and if there is no - it should hide by start date.
Could that be possible?
I have used this code:
/**
* Update the query to use specific post types.
*
* @since 1.0.0
* @param \WP_Query $query The WordPress query instance.
*/
function my_query_by_post_types( $query ) {
$query->set( 'post_type', [ 'arrangement' ] );
}
add_action( 'elementor/query/my_custom_filter', 'my_query_by_post_types' );
/**
* Update the query by specific post meta.
*
* @since 1.0.0
* @param \WP_Query $query The WordPress query instance.
*/
function my_query_by_post_meta( $query ) {
// Get current meta Query
$meta_query = $query->get( 'meta_query' );
// If there is no meta query when this filter runs, it should be initialized as an empty array.
if ( ! $meta_query ) {
$meta_query = [];
}
// Append our meta query
$meta_query[] = [
'key' => 'start_dato',
'value' => date("Y-m-d",time()),
'compare' => '>=',
];
$query->set( 'meta_query', $meta_query );
}
add_action( 'elementor/query/my_custom_filter', 'my_query_by_post_meta' );
I am also interested in this 🙂
Hello,
The relation OR might help you in this case. For example:
// Append our meta query
$meta_query = [
'relation' => 'OR',
[
'key' => 'end_dato',
'value' => date("Y-m-d",time()),
'compare' => '<=',
],
[
'key' => 'start_dato',
'value' => date("Y-m-d",time()),
'compare' => '>=',
]
];
Read more about custom field parameter in the WordPress documentation https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters
Didn't work 🙁
Hello,
Please share your site credentials via this contact form https://metabox.io/contact/
I will take a closer look.
Hello,
You are using this code to get the date
date( 'Y-m-d', current_time( 'timestamp' ) ),
which has the format 2023-10-29, so the field must have the same value format to compare. I fixed the issue by using the option "Save format" of the field, screenshot https://monosnap.com/file/cxYMM0pi2vT8K5l40FRFDbH6HjlE5V
Hi Peter,
This is still wrong.
It shows event there has passed? it shows event with end-date 2023-10-25? Its 5 days ago.
And it didnt show events there is having today as end date?
It should also show date like "30 okt. 2023"
Hello,
It depends on your compare operator in the meta query. You can change the operator to greater like this:
'key' => 'slut_dato',
'value' => date( 'Y-m-d', current_time( 'timestamp' ) ),
'compare' => '>=',
Also, I see it works with the date format Y-m-d
No its not working
Peter, you have fully login - would you please check?
It didnt work. Sorry
Hello,
I add the code above to your site https://support.metabox.io/topic/meta-query-elementor/?swcfpc=1#post-43704
and see it works correctly. The logic here is: show the posts if the end date or start date is greater than the current date. It's an example of using two conditions.
If it is not your logic, you can follow the WordPress documentation to create your own code.
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters