I have an “Actions” page (an archive for a custom post type called Actions), which is an equivalent to “events”:

www.montasserdrissi.com/uecse/actions

I used this tutorial to filter them with custom fields: ville (city), action (type of action) and date and it works perfectly.

Then I created another function to filter by “date range”: past, future actions. And it also works… but it cancels the other.

So these two functions work, but when I put them both, only the second one works.

Can someone please help me figure this out ?
Thank you!

Function to filter by Type/City/Date custom fields:

// array of filters (field key => field name)
$GLOBALS['my_query_filters'] = array( 
    'action'  => 'action', 
    'ville'   => 'ville', 
    'date'    => 'date',

);

// action
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);

function my_pre_get_posts( $query ) {

    // bail early if is in admin
    if( is_admin() ) return;

    // bail early if not main query
    // - allows custom code / plugins to continue working
    if( !$query->is_main_query() ) return;

    // get meta query
    $meta_query = $query->get('meta_query');

    // loop over filters
    foreach( $GLOBALS['my_query_filters'] as $key => $name ) {

        // continue if not found in url
        if( empty($_GET[ $name ]) ) {

            continue;

        }

        // get the value for this filter
        // eg: http://www.website.com/events?city=melbourne,sydney
        $value = explode(',', $_GET[ $name ]);


        // append meta query
        $meta_query[] = array(
            'key'       => $name,
            'value'     => $value,
            'compare'   => 'IN',
        );

    } 

    $query->set( 'orderby', array( 'date' => 'DESC' ) );

    // update meta query
    $query->set('meta_query', $meta_query);

    return;

}

?>

Function for Future/Past actions:

<?php

add_action('pre_get_posts', 'future_past_actions', 10, 1);

function future_past_actions( $query ) {

// bail early if is in admin
    if( is_admin() ) return;

    // bail early if not main query
    // - allows custom code / plugins to continue working
    if( !$query->is_main_query() ) return;

    // get meta query
    $meta_query = $query->get('meta_query');


    if( isset($_GET['range']) ) {

    $range = explode(',', $_GET['range']);

    $meta_query[] = array(
        'key'     => 'range',
        'value'   => $range,
        'compare' => 'IN',
        );


        if( $query->get( 'range' ) == 'future' ) {
             $meta_query[] = array(
             'key' => 'date',
             'value' => date("Ymd"),
             'compare' => '>',
            );
            $meta_query['relation'] = 'OR';
        }

        elseif( $query->get( 'range' ) == 'past' ) {
             $meta_query[] = array(
             'key' => 'date',
             'value' => date("Ymd"),
             'compare' => '<',
            );
            $meta_query['relation'] = 'OR';
        }

        elseif( $query->get( 'range' ) == 'toutes' ) {
           return;
        }

    }

    $query->set( 'meta_key', 'date' );
    $query->set( 'orderby', array( 'date' => 'DESC' ) );
    $query->set('meta_query', $meta_query);

    return;

    }

    ?>

Read more here: 2 pre_get_posts functions to filter by custom fields


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: