Support

Account

Home Forums Feedback Using pre_get_posts to filter only posts that include all values Reply To: Using pre_get_posts to filter only posts that include all values

  • FYI function should look like this:

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

    $meta_query = array(
    ‘relation’ => ‘AND’,
    );
    foreach ($GLOBALS[‘my_query_filters’] as $key => $name) {

    if (!empty($_GET[$name])) {

    $values = explode(‘,’, $_GET[$name]);

    $meta_query[] = array (
    ‘key’ => $name,
    ‘value’ => $values,
    ‘compare’ => ‘IN’,
    );

    }
    }

    $query->set(‘meta_query’, $meta_query);

    return;