• Resolved designemesis

    (@designemesis)


    Hi,

    So I followed the guide to add ‘adverts_category’ to the search panel.. no problem.

    I have a new custom field I’m trying to add to the search panel but I’m having problems with the query part.

    Registering the field on the search box is easy enough:

    add_filter( 'adverts_form_load', 'search_by_condition_form_load' );
    function search_by_condition_form_load( $form ) {
    	
            $newvalue = "New";
    	$usedexc = "Used - Excellent";
    	$usedgood = "Used - Good";
    	$usedavg = "Used - Average";
    	$usedfair = "Used - Fair";
    	$usedpoor = "Used - Poor";
    
        if( $form['name'] != 'search' ) {
            return $form;
        }
        $form['field'][] = array(
            "name" => "adverts_condition",
            "type" => "adverts_field_select",
            "order" => 21,
            "label" => __("Condition", "adverts"),
            "max_choices" => 1,
             "options" =>  array(
    				array( "value" => $newvalue, "text" => $newvalue, "depth" => 1 ),
    				array( "value" => $usedexc, "text" => $usedexc, "depth" => 1 ),
    				array( "value" => $usedgood, "text" => $usedgood, "depth" => 1 ),
    				array( "value" => $usedavg, "text" => $usedavg, "depth" => 1 ),
    				array( "value" => $usedfair, "text" => $usedfair, "depth" => 1 ),
    				array( "value" => $usedpoor, "text" => $usedpoor, "depth" => 1 ),
       	),  
       
            "meta" => array(
                "search_group" => "visible",
                "search_type" => "full" 
    			
            )
        );
        return $form;
    }

    The data is being stored in wp_postmeta it seems. I’m struggling to get the meta_query to work as it’s returning no results. I want ‘value’ to be whatever is selected and then search for posts in the DB:

    add_filter( 'adverts_list_query', 'search_by_condition_query' );
    
    function search_by_condition_query( $args ) {
        
        if( ! adverts_request( "adverts_condition" ) ) {
    		
            return $args;
        }
        
        $args["meta_query"] = array(
            array(
              
                'key'    => 'meta_value',
                'value' => ' adverts_request( "adverts_condition" ),
    			 'compare' => 'IN'
            ),
    		
        );
        
        return $args;
    }	
    	

    Thanks again!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi, what is the name of a meta field in which the condition is saved, or what is the name of the “condition” field in the [adverts_add] form?

    If it is “adverts_condition” then i think that the line

    
    'key'    => 'meta_value',
    

    should be

    
    'key'    => 'adverts_condition',
    
    Thread Starter designemesis

    (@designemesis)

    Hey Greg,

    Thanks very much this worked!

    I have another question about how the search works.
    Right now if I select category & location the results returned are only categories in that particular location.. which is right.

    If I select category + location + condition it is returning all items of that condition even if the location and category are not correct.

    How can I make it so my condition field behaves like category/location?
    It should be like AND but it’s behaving like an OR.. if that makes sense.

    Thanks again!

    Plugin Author Greg Winiarski

    (@gwin)

    Hi, this is because your code overwrites the whole meta_query, to append additional query param to the search, the code below

    
    $args["meta_query"] = array(
        array(
            'key'    => 'adverts_condition',
            'value' => ' adverts_request( "adverts_condition" ),
            'compare' => 'IN'
        ),
    );
    

    should be

    
    $args["meta_query"][] = array(
        'key'    => 'adverts_condition',
        'value' => ' adverts_request( "adverts_condition" ),
        'compare' => 'IN'
    );
    
    Thread Starter designemesis

    (@designemesis)

    Works great thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search by custom field issues (meta_query)’ is closed to new replies.