Order by date

Support MB Custom Post Type Order by date

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #45598
    info@ivanit.nl[email protected]
    Participant

    I followed this tutorial: https://docs.metabox.io/tutorials/show-upcoming-events-with-elementor/

    The custom query is different in the video then the text shown, but I want the custom posts to sort by date.

    Also my date format is different: d-m-Y

    I came up with this query:

    /* Filter Upcoming Event */
    add_action('elementor/query/filter_events', function($query) {
    $current_datetime = current_datetime()->format('d-m-Y');
        $query->set('post_type', ['event']);
        $meta_query = [
            'Relations' => 'OR',
            [
                'key' => 'startdate',
                'value' => date($current_datetime),
                'compare' => '>=',
            ],
            [
                'key' => 'enddate',
                'value' => date($current_datetime),
                'compare' => '>=',
                ],
        ];
    	$query->set( 'meta_key', 'startdate' );
    	$query->set( 'orderby', 'meta_value' ); // for numeric meta use <code>meta_value_num</code> instead of <code>meta_value</code>
        $query->set( 'order', 'ASC' );
        $query->set('meta_query', $meta_query);
    });
    

    But the date sort is not working, not on frontend or de admin column startdate in the backend gives the same sort.

    What is going wrong here?

    Kind regards,

    Ivan

    #45607
    PeterPeter
    Moderator

    Hello Ivan,

    The code is hooked to the Elementor action so it won't work in the admin area, column startdate. You can remove the meta query $query->set('meta_query', $meta_query); of the example code and keep your own code to sort by startdate field.
    The date format should have the same field format that is saved to the database.

    Please follow the documentation https://docs.metabox.io/fields/date/#date-format

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