Custom search fields not working
-
Hi there,
I’ve followed steps given here: http://wpadverts.com/documentation/custom-fields-search-form/ however still custom search fields don’t work.
Our site: http://housesforrent.sydney/ , I’ve removed them at the moment because they were not working.Could you please look into it and suggest?
Thanks.
-
Hi, can you share code you are using for custom search and what do you mean by not working (fields do not show or searching by them does nothing)?
Hi Greg,
Thanks for your reply.
The fields show, but searching by them doesn’t work.
I’ve used below code as suggested on above documentation link:
add_filter( 'adverts_form_load', 'search_by_category_form_load' ); /** * Adds category dropdown into search form in [adverts_list]. * * @param array $form Search form scheme * @return array Customized search form scheme */ function search_by_category_form_load( $form ) { if( $form['name'] != 'search' ) { return $form; } $form['field'][] = array( "name" => "advert_category", "type" => "adverts_field_select", "order" => 20, "label" => __("Category", "adverts"), "max_choices" => 10, "options" => array(), "options_callback" => "adverts_taxonomies", "meta" => array( "search_group" => "visible", "search_type" => "full" ) ); return $form; }add_filter( 'adverts_list_query', 'search_by_category_query' ); /** * Adds tax_query param to WP_Query * * The tax_query is added only if it is in $_GET['advert_category'] * * @param array $args WP_Query args * @return array Modified WP_Query args */ function search_by_category_query( $args ) { if( ! adverts_request( "advert_category" ) ) { return $args; } $args["tax_query"] = array( array( 'taxonomy' => 'advert_category', 'field' => 'term_id', 'terms' => adverts_request( "advert_category" ), ), ); return $args; }It is used here: http://housesforrent.sydney/ I’ve put them for your review.
Please let me know in case of any other questions.
Thanks.In the first code replace
"name" => "advert_category",with
"name" => "ad-category",in the second code replace both
adverts_request( "advert_category" )with
adverts_request( "ad-category" )then try searching again.
Was struggling with this for the last 5 hours… Just chanced to see this as I was writing you a thank-you note, Greg.
Greg, you owe Rajdeep & me a coffee. Of course, it will be a pleasure to host to here in India, when you come for that coffee. 🙂
Hi Greg,
Thank you so much for the help. That worked.
However, I have used many custom fields (you may check here: http://housesforrent.sydney/post-ads/ ). Most of them are radio boxes and text fields.
Could you please check and confirm what I am doing wrong in this code? It doesn’t seem to work.
{"type" => "adverts_field_select" } $form["field"][] = array( "name" => "smoking1", /* This is my custom field name */ "type" => "adverts_field_select" "order" => 25, "label" => "Smoking", "is_required" => false, "validator" => array( ), "max_choices" => 2, "options" => array( array("value"=>"Yes", "text"=>"Yes"), array("value"=>"No", "text"=>"No"), ), "meta" => array( "search_group" => "visible", "search_type" => "half" ) );Could you please also confirm where exactly I need to change in case I want to include text box or radio boxes in the below code:
function search_by_category_query( $args ) { if( ! adverts_request( "ad-category" ) ) { return $args; } $args["tax_query"] = array( array( 'taxonomy' => 'advert_category', 'field' => 'term_id', 'terms' => adverts_request( "ad-category" ), ), ); return $args; }Your second function should be something like
add_filter( 'adverts_list_query', 'search_by_custom_fields' ); function search_by_custom_fields( $args ) { $cf = array("smoking1", "furnished"); // <- more fields here foreach($cf as $name) { if( ! adverts_request( $name ) ) { continue; } if( ! isset( $args["meta_query"] ) ) { $args["meta_query"] = array(); } $args['meta_query'][] = array( 'key' => $name, 'value' => adverts_request( $name ), ); } return $args; }Your custom fields are most likely saving data as meta data so your search function needs to query meta data fields not taxonomies.
Hi Rajdeep,
A request please.
Can you pl share the exact code you used for searching any one of the custom fields you used (adding the search box as well as for doing actual search)
I seem to be doing something wrong, my display is not coming correctly.
I am not able to insert meta data piece of code.Before bothering Greg again, I want to check our codes apple to apple.
Many thanks in advance.
Hi there,
I used the code as me and Greg have pasted above. There were some issues and Greg was great in helping with that.
Thanks.
Hi Greg,
Thank you so much for your help so far. There is one last issue where I am still having trouble.
I am not sure where exactly we replace the dropdown “Select Options” text in the custom search fields. I want it to be something like “Select Internet availability”, “Select Furniture Options” etc., Here is the link for your reference: http://housesforrent.sydney/
Thanks a lot.
Hi, if you are using multiselect field then i am afraid that right now it is not really possible to change this, as this text is hardcoded for all multiselect fields, i will update it in next release.
Hi Greg,
Thanks for your quick reply.
Is it possible if we use single select field? I used below code and single select field didn’t work:
$form["field"][] = array( "name" => "smoking1", "type" => "adverts_field_select", "order" => 25, "label" => __("Smoking", "adverts"), "max_choices" => 1, "options" => array( array("value"=>"Yes", "text"=>"Yes"), array("value"=>"No", "text"=>"No"), ), "meta" => array( "search_group" => "visible", "search_type" => "half" ) );Your suggestions would be highly appreciated.
Thanks.
Hi,
with single select this should be possible, in your array you need to add follwoing keys “empty_option” (boolean) and “empty_option_text” (string).$form["field"][] = array( "name" => "smoking1", "type" => "adverts_field_select", "order" => 25, "label" => __("Smoking", "adverts"), "empty_option" => true, "empty_option_text" => "My Empty Option", "max_choices" => 1, "options" => array( array("value"=>"Yes", "text"=>"Yes"), array("value"=>"No", "text"=>"No"), ), "meta" => array( "search_group" => "visible", "search_type" => "half" ) );
The topic ‘Custom search fields not working’ is closed to new replies.