Better and easier search for users
When you select a category or enter a search criteria in search bar, there will be a refine search form on sidebar with which you can refine your search results. This is very nice, but it has some downsides.
My users needed a from – to search and not just for price, but for custom fields also. So I decided to modify this and create a better search for my customers.
Here is a screenshot for you to see how this works.
And here is the output:
You can also try it out on a live page here. The page is not in English, but you will figure it out.
Let’s begin with the modification.
This mod is very easy and you have to modify only one file:
Classipress 3.x: classipress/includes/search.php
Classipress 4.x: classipress/includes/views/class-ads-search.php.
First make a backup of this file, in case something goes wrong.
Then open it and locate this part of code:
default: if ( 'cp_' == substr( $key, 0, 3 ) ) { $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->cp_ad_fields WHERE field_name = %s", $key ) ); if ( $field === null ) break; $compare = ( in_array( $field->field_type, array( 'radio', 'checkbox', 'drop-down' ) ) ) ? 'IN' : 'LIKE'; $meta_query[] = array( 'key' => $key, 'value' => $value, 'compare' => $compare ); } break;
And change it to:
default: if ( 'cp_' == substr( $key, 0, 3 ) ) { $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->cp_ad_fields WHERE field_name = %s", $key ) ); if ( $field === null ) break; if ( strpos($value, '-') ) { $minusPos = strpos($value, '-'); $value = array( trim(substr($value, 0, $minusPos)), trim(substr($value, $minusPos+1, strlen($value) ) ) ); $meta_query[] = array( 'key' => $key, 'value' => $value, 'compare' => 'BETWEEN', 'type' => 'numeric' ); } else { $compare = ( in_array( $field->field_type, array( 'radio', 'checkbox', 'drop-down' ) ) ) ? 'IN' : 'LIKE'; $meta_query[] = array( 'key' => $key, 'value' => $value, 'compare' => $compare ); } } break;
If still using Classipress 3.x find this code (around line 619):
default : if ( 'cp_' == substr( $key, 0, 3 ) ) { $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->cp_ad_fields WHERE field_name = %s", $key ) ); if ( $field === null ) break; $refine_count++; if ( is_array( $value ) ) $value = implode( "','", $value ); $compare = ( in_array( $field->field_type, array( 'radio', 'checkbox', 'drop-down' ) ) ) ? "IN ('$value')" : "LIKE '%$value%'"; $query .= " AND ("; $query .= "(mt" . $refine_count . ".meta_key = '$key')"; $query .= " AND (CAST(mt" . $refine_count . ".meta_value AS CHAR) $compare)"; $query .= ")"; } break;
And change it to:
default : if ( 'cp_' == substr( $key, 0, 3 ) ) { $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->cp_ad_fields WHERE field_name = %s", $key ) ); if ( $field === null ) break; $refine_count++; if ( strpos($value, '-') ) { $minusPos = strpos($value, '-'); $value = array( trim(substr($value, 0, $minusPos)), trim(substr($value, $minusPos+1, strlen($value) ) ) ); $compare = "BETWEEN '$value[0]' AND '$value[1]'"; } else { if ( is_array( $value ) ) $value = implode( "','", $value ); $compare = ( in_array( $field->field_type, array( 'radio', 'checkbox', 'drop-down' ) ) ) ? "IN ('$value')" : "LIKE '%$value%'"; } $query .= " AND ("; $query .= "(mt" . $refine_count . ".meta_key = '$key')"; $query .= " AND (CAST(mt" . $refine_count . ".meta_value AS CHAR) $compare)"; $query .= ")"; } break;
Your search should now be ready to receive FROM-TO values.
Add additional text
You can also add some additional text to let users know they can use FROM-TO values to search.
You can use plugin called Easy CP Tools or do this manually.
Locate this part of code:
<div class="pad10"> </div> <p><button id="go" class="obtn btn_orange" tabindex="1" name="sa" type="submit" value="Go"><!--?php _e( 'Refine Results &rsaquo;&rsaquo;', APP_TD ); ?--></button></p> |
and before or after that add:
<div class="pad10"> </div> <p><strong>FROM-TO</strong> values are also possible</p> |
Compatibility
This modification has been tested on Classipress from version 3.3.2 to 4.x.
If you find any issues with this tutorial, please be so kind and write a comment. I’ll try to help as much as I can.
Nice, this is what we wanted. Is there a screenshot or live demo to take a look please
I have added screenshots and a link of working site to the post for everyone to see.
Thank you so much for your quick response. cheers
I have added the code you mention, but I get the following error strpos() expects parameter 1 to be string, array given in public_html/wp-content/themes/classipress/includes/search.php on line 372.
When ever I add a drop down field as a search filter and select one of the values I am getting that mistake. I am using flatpress as my child theme .
I have tried it on my installation and I’m not getting this error. Let’s try this:
If this is the line that you are getting an error
if ( strpos($value, ‘-‘) ) {
change it to this
if ( !is_array($value) && strpos($value, ‘-‘) ) {
and let me know. If it still doesn’t work, please send my your search.php file and I’ll check.
Regards
hi Blaz, can you add state search to search bar near all categories in classipress 3.3?
thnx
Sorry for late response. I will take a look at it and let you know.
hi, thank u for sharing this mod. i have edited file but there is no change to search bar. i am using classipress 3.5.1
Hi,
This mod is not for Search Bar, but for Search Refine. It’s a widget on a category or after you input some search criteria.
Is this what you were looking for?
Kind regards,
Blaz
Nice, it works!
I use it to filter ads by year.
Do u think it’s posibile to use the slider from the “Price”. I tried but faild :(.
This requires also js to be modified and would require much more changes. Keep in mind that when a new version of CP arrives, all this has to be done again. That’s why I’m keeping modifications to a minimum.
hi please how we can change the search bar to filtred bar ( search by city years à ..
This is not a small modification. It requires quite some programming. I’ll see if I can make it as a plugin.
Regards.
Hi
this is exactly what i was looking for.
can we have the search in two boxes. i mean one for min value, the other for max value (cm, or m2…) ?
and what about multi levels for instance car brand–> car models is it doable ?
thanks
Hi,
It is not that simple to change that, since this will require changes on more than one file and then you would have more work after every upgrade. A child theme with this functionality would be a better choice.
Hi Blaz,
it works well with CP3. Would you creat a description about that how to make this under CP4? I have more site and they all work with CP4.
Thanks and regards
Joe