WordPress custom search, search by meta key

I have a search form with two dropdowns showing custom terms and two text fields for custom meta keys, The search work with tax_query but meta_query has no result.

Here is the pre_get_posts hooks callback function.

Why didn't get results through meta keys filters?

function sm_pre_get_posts( $query ) {

    // check if the user is requesting an admin page 
    // or current query is not the main query
    
    if ( is_admin() || ! $query-is_main_query() ){
        return;
    }

  
    // edit the query only when post type is 'listing'
    // if it isn't, return
    if ( !is_post_type_archive('listing') ){
         return;
    }
 
$meta_query = array();
    

    
    // $query-set( 'category_name', 'carlo' );
    
    
//$meta_query[] = array( 's' = '' );
    // add meta_query elements

     

    
    
 if( !empty( get_query_var( 'listing_loc' ) ) ){
     
            $taxquery[] = array(
                array(
                    'taxonomy' = 'listing_location',
                    'field' = 'slug',
                    'terms' = esc_attr( get_query_var( 'listing_loc' ) ),
                )
            );
        }
        
        
if( !empty( get_query_var( 'listing_program' ) ) ){ 
        
        $taxquery[] = array(
            array(
                'taxonomy' = 'listing_prog',
                'field' = 'slug',
                'terms' = get_query_var( 'listing_program' ),
            )
        );
        
    }
    
    
   $query-set( 'tax_query', $taxquery );
    
if( !empty( get_query_var( 'loan_min' ) ) || !empty( get_query_var( 'loan_max' ) ) ){
        
 if( !empty( get_query_var( 'loan_min' ) ) ) 
 $loan_min = get_query_var( 'loan_min' );
if( !empty( get_query_var( 'loan_max' ) ) ) $loan_max = get_query_var( 'loan_max' );
        
if( !empty( get_query_var( 'loan_min' ) )   empty( get_query_var( 'loan_max' ) ) ) 
         { $loan_max = get_query_var( 'loan_min' ); $loan_min = get_query_var( 'loan_min' ); }
         
  if( !empty( get_query_var( 'loan_max' ) )   empty( get_query_var( 'loan_min' ) ) )
{ $loan_max = get_query_var( 'loan_max' ); $loan_min = get_query_var( 'loan_max' ); }
         
         
        
            $args = array(
                   
                      'relation' = 'AND',
                       array(
                         'key'     = 'listing_loan_amount_minimum',
                         'value'   = array($loan_min,$loan_max),
                         'compare' = 'between',
                         'type'    = 'numeric', 
                       ),
                       array(
                         'key'     = 'listing_loan_amount_maximum',
                         'value'   =  array($loan_min,$loan_max),
                         'compare' = 'between',
                         'type'    = 'numeric', 
                       ),
                    
                );
                
            
        
          $query-set( 'meta_query', $args );
          
         
    
    }   
    
    
}
add_action( 'pre_get_posts', 'sm_pre_get_posts', 1 );

Topic meta-query meta-value pre-get-posts post-meta custom-field Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.