Support

Account

Home Forums Front-end Issues Menu Missing With Custom Filter

Solved

Menu Missing With Custom Filter

  • Hi,

    I am following the guide at Creating a WP archive with custom field filter.

    It’s working great but after filtering, the main menu goes missing. As seen at this link.

    The following is the code that I modify

    add_action('pre_get_posts', 'my_pre_get_posts');
    
    function my_pre_get_posts( $query){
    
      //validate
      if (is_admin()){ return; }
    
      // get original meta query
      $meta_query = $query -> get('meta_query');
    
      // allow the url to alter the query
      // eg. ?district=1
      // eg. ?district=2
      if (isset($_GET['district']) ){
    
        $district = $_GET['district'];
        $range = explode(' - ', str_replace('$', '', $_GET['price-range']));
    
        //add meta query to the original meta queries
        $meta_query[] = array(
            'relation'  => 'AND',
            array(
            'key'       => 'district',
            'value'     => $district,
            'compare'   => 'IN',
            ),
            array(
            'key'       => 'price',
            'value'     => $range[0],
            'type'      => 'NUMERIC',
            'compare'   => '>'
            ),
            array(
            'key'       => 'price',
            'value'     => $range[1],
            'type'      => 'NUMERIC',
            'compare'   => '<'
            )
        );
    
      }
    
      // update the meta query args
      $query -> set('meta_query', $meta_query);
    
      //always return
      return;
    }

    I read that it is due to is_main_query but I have no idea how to integrate it.

    My php skill is alright but I am WordPress amateur.

  • I have narrowed it down that the district is the problem. When I am filtering without the district the menu appear. Specifically if I remove the district%5B%5D, the menu appear but with it then it will disappear.

  • Nevermind I found my solution here

    I just need to wrap the function with if $query->is_main_query() statement.

     if ($query->is_main_query()){
      // get original meta query
      $meta_query = $query -> get('meta_query');
    
      // allow the url to alter the query
      // eg. ?district=1
      // eg. ?district=2
      if (isset($_GET['district']) ){
    
        $district = $_GET['district'];
        $range = explode(' - ', str_replace('$', '', $_GET['price-range']));
    
        //add meta query to the original meta queries
        $meta_query[] = array(
            'relation'  => 'AND',
            array(
            'key'       => 'district',
            'value'     => $district,
            'compare'   => 'IN',
            ),
            array(
            'key'       => 'price',
            'value'     => $range[0],
            'type'      => 'NUMERIC',
            'compare'   => '>'
            ),
            array(
            'key'       => 'price',
            'value'     => $range[1],
            'type'      => 'NUMERIC',
            'compare'   => '<'
            )
        );
    
      }
    
      // update the meta query args
      $query -> set('meta_query', $meta_query);
    
      //always return
      return;
      }
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Menu Missing With Custom Filter’ is closed to new replies.