Meta Query - Elementor
- This topic has 10 replies, 3 voices, and was last updated 1 year, 5 months ago by
Peter.
-
AuthorPosts
-
October 23, 2023 at 5:51 PM #43585
NPDK
ParticipantHi,
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' );
October 24, 2023 at 1:04 AM #43594Duffl
ParticipantI am also interested in this 🙂
October 24, 2023 at 10:24 PM #43600Peter
ModeratorHello,
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
October 25, 2023 at 12:29 AM #43602NPDK
ParticipantDidn't work 🙁
October 26, 2023 at 9:30 PM #43645Peter
ModeratorHello,
Please share your site credentials via this contact form https://metabox.io/contact/
I will take a closer look.October 29, 2023 at 10:42 PM #43669Peter
ModeratorHello,
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/cxYMM0pi2vT8K5l40FRFDbH6HjlE5VOctober 31, 2023 at 6:29 PM #43694NPDK
ParticipantHi 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"
October 31, 2023 at 10:09 PM #43704Peter
ModeratorHello,
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
October 31, 2023 at 10:43 PM #43707NPDK
ParticipantNo its not working
November 3, 2023 at 3:12 PM #43727NPDK
ParticipantPeter, you have fully login - would you please check?
It didnt work. SorryNovember 4, 2023 at 9:39 PM #43740Peter
ModeratorHello,
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 -
AuthorPosts
- You must be logged in to reply to this topic.