0

I am running a wordpress site with the Essential Grid plugin to display links to posts that I create. The posts are events that take place over an amount of time (days/weeks/months). I have created some custom meta fields for "eg-startdate" and "eg-enddate" and I now need to tell the grid that I only want to display posts where:

eg-startdate <= today AND eg-enddate >= today

I have been able to filter the grid to only show events where eg-enddate >= today using the following code:

function eg_mod_query($query, $grid_id){

if($grid_id == 9) {

    $query['meta_query'] = array(
    'key'     => 'eg-enddate',
    'value'   => date("d/m/Y"),
    'compare' => '>',
    'type'    => 'numeric'
    );
    $query['meta_key'] = 'eg-enddate';
    $query['meta_value'] = date("d/m/Y");
    $query['meta_value_num'] = date("d/m/Y");
    $query['meta_compare'] = '>';

}

return $query;

}

How do I now modify this code, so that as well as limiting posts where 'eg-enddate' is in the future, it also filters posts where 'eg-startdate' is in the past-or-today, whilst ensuring that all data is ultimately stored within $query?

Appreciate any help, thank you!! :)

1 Answer 1

0

You can pass array with multiple queries to meta_query.

Something like:

'meta_query' => array(
        array(
            'key'     => 'eg-enddate',
            'value'   => date("d/m/Y"),
            'compare' => '>',
            'type'    => 'numeric'
        ),
        'relation' => 'AND',
        array(
            'key'     => 'eg-startdate',
            'value'   => date("d/m/Y"),
            'compare' => '<',
            'type'    => 'numeric'
        )
    )

For more please go through documentation: https://codex.wordpress.org/Class_Reference/WP_Meta_Query

Note: Code is not tried or tested so look out for syntax errors.

1
  • Thank you!! It's so simple when you see the answer, unfortunately, multi-dimensional associative arrays and I don't seem to get along too well! Really appreciate the fast response and perfect answer! +1 Jan 24, 2017 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.