0

I’m working with Genesis Theme for a while but never got any problems:)

This is my first big issue and I dont know how to solve it. I’m making a new website with Custom Post Types and WP All Import plugin. Everything is working fine but I made some kind of filter to filter by a custom post type. Filter is working as it should be but after page reload my Main menu is gone.

This is the page I mean. (not live yet so you may see some bugs)

Without filter and with menu: With Menu

With filter and without menu: Without Menu

Someone has an idea why this is happening and how I can solve this issue?

Thanks in advance.

Kind regards,

Joep

This is the filter in my functions.php

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

function my_pre_get_posts( $query ) {

    // bail early if is in admin
    if( is_admin() ) {

        return;

    }


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

    if(!empty($_GET['contract']))
    {
        $contract = explode(',', $_GET['contract']);
        $meta_query[] = array(
        'key' => 'contract',
        'value' => $_GET['contract'],
        'compare' => 'IN',
        );
    }

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

    return;
}
  • what does your filter look like? remember that query filters run for every query on a page, including ones that load menu items. you have to target them correctly. – Milo Aug 25 '15 at 17:44
  • Hello, thx for the fast reply, the script is in my post now – user2812779 Aug 25 '15 at 17:58
0

pre_get_posts filters will be applied to all queries on a page- the main query, menus, and additional WP_Query instances.

To target only the main query, use is_main_query():

function my_pre_get_posts( $query ) {

    // bail early if is in admin
    // or if query is not main query
    if( is_admin() || !$query->is_main_query() ) {
        return;
    }

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

    if(!empty($_GET['contract']))
    {
        $contract = explode(',', $_GET['contract']);
        $meta_query[] = array(
        'key' => 'contract',
        'value' => $_GET['contract'],
        'compare' => 'IN',
        );
    }

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

    return;
}
  • This did the trick! Thanks for your good explanation and help!:) – user2812779 Aug 26 '15 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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