Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Customize WP Search WordPress

  • SOLVED

I want to customize the wordpress search on my site.
Each of my WP pages are titled by a 6 digit number (e.g. 100028).
I want to REQUIRE a visitor to type in a 6 digit number before my search will return any results and I only want the "exact match" to show (exact page).
Is this possible? Any widgets or codes that exist?
Thanks for any help.

Answers (4)

2012-10-04

John Cotton answers:

I think this will do what you want. Add to your functions.php file.:


function my_search_filter($where) {

if( $search = get_query_var('s') ) {
$where = "AND wp_posts.post_title = '$search'";
}
return $where;
}
add_filter('posts_where','my_search_filter');


It will trap ever query just before it's run and - if it's a search (assuming its the standard search box which is called s) will fix the where clause to pull a post of the exact name of the search.

So all you need do is dress up the standard search box with some html to tell uses to put in their "code".


DJeXtol comments:

Thank John!
This worked but for some reason when it returned the search it would repeat the same result a number of times. But it did do the job. I really appreciate it.

2012-10-05

Arnav Joy answers:

try adding following code to functions.php

function search_where_filter($where) {



if( $search = get_query_var('s') ) {

$where = "AND wp_posts.post_title = '$search' AND wp_posts.post_type = 'page' ";

}

return $where;

}

add_filter('posts_where','search_where_filter');


function search_posts_filter( $query ){
if ($query->is_search){
$query->set('post_type',array('page'));
}
return $query;
}
add_filter('pre_get_posts','search_posts_filter');


DJeXtol comments:

Thank Arnav!
This was the best option. It worked and only returns 1 result.
I really appreciate it.


Arnav Joy comments:

thanks
can you vote now and close the question?

2012-10-04

Angel answers:

You can try these plugins I'm currently using search-everything and it's a good plugin.

http://wordpress.org/extend/plugins/search-everything/

http://wordpress.org/extend/plugins/relevanssi/

2012-10-05

Francisco Javier Carazo Gil answers:

You can also control with JavaScript that your users introduce only 6 digits. You have to include a simple JS code.

First, create a function to check if it is a number:
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}

Later check lenght: if(search.lenght == 6 && isNumber(search)) and you will have this OK.


DJeXtol comments:

Thanks Francisco for the replay. I couldn't get this to work and it shut my site down when I tried. Maybe I wasn't posting it in the right place. I got the site back up and running now.
Thanks for your time.