Results 1 to 7 of 7

Thread: Filter search by custom parameter and meta query

  1. #1
    Thread Starter
    Amateur emilianoaguirre's Avatar
    Join Date
    May 2012
    Location
    Argentina
    Posts
    14
    Thanks
    10
    Thanked 1 Time in 1 Post

    Filter search by custom parameter and meta query

    Hello everyone, I've searched this a lot and couldn't find a solution.

    I have modified the search form to inlclude a custom parameter, and It return a URL like this:

    Code:
    /?s=carpintero&zona=Berazategui
    then I need to compare it with a custom field trying this code in my function.php (child theme):

    Code:
    add_action( 'pre_get_posts', 'search_filter' );
    function search_filter($query) {
    
      if ( !is_admin() && is_search() && $query->is_main_query() ) {
    
        if ( isset($_GET['zona']) && !empty($_GET['zona']) ){
    
            //Get original meta query
            $meta_query = $query->get('meta_query');
    
            //Add our meta query to the original meta queries
            $meta_query[] = array(
                'key'     => 'cp_zonas',
                'value'   => $_GET['zona'],
                'compare' => '='
            );
    
            $query->set('meta_query',$meta_query);
    
        }
    
      }
    
    }
    I have debug mode on and prints this:

    Code:
    Array
    (
        [s] => carpintero
        [error] => 
        [m] => 0
        [p] => 0
        [post_parent] => 
        [subpost] => 
        [subpost_id] => 
        [attachment] => 
        [attachment_id] => 0
        [name] => 
        [static] => 
        [pagename] => 
        [page_id] => 0
        [second] => 
        [minute] => 
        [hour] => 
        [day] => 0
        [monthnum] => 0
        [year] => 0
        [w] => 0
        [category_name] => 
        [tag] => 
        [cat] => 
        [tag_id] => 
        [author_name] => 
        [feed] => 
        [tb] => 
        [paged] => 0
        [comments_popup] => 
        [meta_key] => 
        [meta_value] => 
        [preview] => 
        [sentence] => 
        [fields] => 
        [menu_order] => 
        [category__in] => Array
            (
            )
    
        [category__not_in] => Array
            (
            )
    
        [category__and] => Array
            (
            )
    
        [post__in] => Array
            (
            )
    
        [post__not_in] => Array
            (
            )
    
        [tag__in] => Array
            (
            )
    
        [tag__not_in] => Array
            (
            )
    
        [tag__and] => Array
            (
            )
    
        [tag_slug__in] => Array
            (
            )
    
        [tag_slug__and] => Array
            (
            )
    
        [post_parent__in] => Array
            (
            )
    
        [post_parent__not_in] => Array
            (
            )
    
        [meta_query] => Array
            (
                [0] => Array
                    (
                        [key] => cp_zonas
                        [value] => Berazategui
                        [compare] => =
                    )
    
            )
    
        [ignore_sticky_posts] => 
        [suppress_filters] => 
        [cache_results] => 1
        [update_post_term_cache] => 1
        [update_post_meta_cache] => 1
        [post_type] => any
        [posts_per_page] => 7
        [nopaging] => 
        [comments_per_page] => 4
        [no_found_rows] => 
        [search_terms] => Array
            (
                [0] => carpintero
            )
    
        [order] => DESC
    )
    So it does add the value to meta_query but the result its the same.

    May be pre_get_posts its not the way ... this should be simplier :(

    Anyone can help me?

  2. #2
    Thread Starter
    Amateur emilianoaguirre's Avatar
    Join Date
    May 2012
    Location
    Argentina
    Posts
    14
    Thanks
    10
    Thanked 1 Time in 1 Post
    You must be an AppThemes customer and logged in to view this response. Join today!

  3. #3
    Expired Customer mcbv's Avatar
    Join Date
    Jul 2015
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  4. #4
    Expired Customer mcbv's Avatar
    Join Date
    Jul 2015
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  5. #5
    Expired Customer elagoon's Avatar
    Join Date
    Mar 2016
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  6. #6
    Veteran wzshop's Avatar
    Join Date
    Mar 2012
    Location
    Netherlands
    Posts
    356
    Thanks
    27
    Thanked 7 Times in 6 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  7. #7
    Veteran wzshop's Avatar
    Join Date
    Mar 2012
    Location
    Netherlands
    Posts
    356
    Thanks
    27
    Thanked 7 Times in 6 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Map / location search filter doesn't work - the post search filter works fine
    By tdelude in forum Report JobRoller Bugs
    Replies: 4
    Last Post: May 23rd, 2013, 11:35 AM
  2. Replies: 1
    Last Post: April 29th, 2013, 07:10 PM
  3. Query custom fields
    By brookss1312 in forum ClassiPress General Discussion
    Replies: 1
    Last Post: February 18th, 2013, 09:54 AM
  4. Custom Field Query
    By ggriffin in forum Vantage General Discussion (Legacy)
    Replies: 0
    Last Post: May 29th, 2012, 05:00 AM