Search code examples
wordpressdatefilterargs

Wordpress filter with multiple parameters



I'm trying to set up a wordpress page in which there is a main section with upcoming events and a secondary section in which there are passed events.
I'm using this filter (located in functions.php) in order to switch upcoming and passed events:
// upcoming events
function listing_filter_function($date_args){
$date_args = array(
                    'post_type'   => 'evento',
                    'meta_key' => 'start_date',
                    'posts_per_page' => -1,
                    'orderby' => 'meta_value_num',
                    'order' => 'ASC',
                    'meta_query'=> array(
                        array(
                          'key' => 'start_date',
                          'compare' => '>=',
                          'value' => date("Ymd"),
                          'type' => 'DATE'
                        )
                    ),
                );
return $date_args; } add_filter('listing_filter', 'listing_filter_function');

// events archive
function archive_filter_function($date_args){
$date_args = array(
                'post_type'   => 'evento',
                'meta_key' => 'start_date',
                'posts_per_page' => -1,
                'orderby' => 'meta_value_num',
                'order' => 'DESC',
                'meta_query'=> array(
                    array(
                      'key' => 'start_date',
                      'compare' => '<',
                      'value' => date("Ymd"),
                      'type' => 'DATE'
                    )
                ),
            );
return $date_args; } add_filter('archive_filter', 'archive_filter_function');

In this filter the events are sorted using the start date, but I would also need to check the end date to prevent events lasting more than one day from being show in the archive section after the first day

Thanks in advance!


Solution

  • you can use the meta_query relation for this like this example

    'meta_query'=> array(
    'relation' => 'AND',
            array(
                'key' => 'start_date',
                'compare' => '>='
                'value' => date("Ymd"),
                'type' => 'DATE'
            ),
            array(
                'key' => 'end_date',
                'compare' => '<'
                'value' => date("Ymd"),
                'type' => 'DATE'
            ),
        )
    

    for the archive you can check if the end_date is smaller then today or equal or less (<=)

    Edit to answer the Question in the comment, this was asked after marking the answer correct. Please note I'm not 100% on this as I'm readying mixed results about this method so if it works please let us know so I can update this to reflect the results

    'meta_query'  => array(
        'relation' => 'OR',
        array(
            'relation' => 'AND',
            array(
                'key' => 'start_date',
                'compare' => '>'
                'value' => date("Ymd"),
                'type' => 'DATE'
            ),
            array(
                'key' => 'end_date',
                'compare' => '<'
                'value' => date("Ymd"),
                'type' => 'DATE'
            ),
        ),
        array(
            'relation' => 'AND',
            array(
                'key' => 'start_date',
                'compare' => '>'
                'value' => date("Ymd"),
                'type' => 'DATE'
            ),
            array(
                'key' => 'end_date',
                'value' => '1',
                'compare' => 'NOT EXISTS'
            ),
        ),
    ),