Search by custom field issues (meta_query)
-
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)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Search by custom field issues (meta_query)’ is closed to new replies.