Meta Query - Elementor

Support General Meta Query - Elementor

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #43585
    NPDKNPDK
    Participant

    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' );
    #43594
    DufflDuffl
    Participant

    I am also interested in this 🙂

    #43600
    PeterPeter
    Moderator

    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

    #43602
    NPDKNPDK
    Participant

    Didn't work 🙁

    #43645
    PeterPeter
    Moderator

    Hello,

    Please share your site credentials via this contact form https://metabox.io/contact/
    I will take a closer look.

    #43669
    PeterPeter
    Moderator

    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

    #43694
    NPDKNPDK
    Participant

    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"

    https://monosnap.com/file/Zso2s9eL4N7CSbOdS1sfWAnFomygku

    #43704
    PeterPeter
    Moderator

    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

    #43707
    NPDKNPDK
    Participant

    No its not working

    #43727
    NPDKNPDK
    Participant

    Peter, you have fully login - would you please check?
    It didnt work. Sorry

    #43740
    PeterPeter
    Moderator

    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

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.