Pre_get_posts function to filter by custom fields

http://www.montasserdrissi.com/uecse/actions

I have an “Actions” page (an archive for a custom post type called Actions), which is an equivalent to “events”, I used this tutorial to filter them with custom fields: ville (city), action (type of action) and date and it works perfectly.

<?php

// 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;

}

?>

Now I’m trying to add two filters to get posts by date range: future and past actions, and I tried many things with my very limited coding skills with no success.

My last failed attempt was to define queryvar “range” and then create a second “pre_get_posts” action to define the values “future” and “past” and modify the query again to filter the posts when the radio button “future” or “past” is checked.

Can someone tell me what I’m doing wrong please? Here’s the second function I created:

<?php

$today = date("Ymd");

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']) ) {

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

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

    if( $query->get( 'range' ) == 'past' ){
         $meta_query[] = array(
         'key' => 'date',
         'value' => $today,
         'compare' => '<',
        );
    }

    $query->set('meta_query', $meta_query);

    return;

}

?>

Thank you.

Read more here: Pre_get_posts function to filter by custom fields

Leave a Reply

Your email address will not be published. Required fields are marked *