[+] Update!
[-] Update!
When I originally created a function to select posts with multiple categories, it became quickly apparent that I would need to update it. For example, many people wanted to be able to search for ANY category selected versus ALL categories selected. Then people asked "what about tags?" and "can you combine search terms?"
Sheesh.
Well, the fact is that I needed the same function for several of my pages, including this one, but especially the image repository that I'm building. For it, I have every post being between one and 10 photos. I will categorize them and tag them, but with over 7000 pics, it can be hard to find what I need so now I have the advanced blog search: Jsearch.
Jsearch
Here's a picture of what it looks like:

And here's how it works:
You enter a term or select a Category or Tag (or multiple terms separated by spaces, or CTRL click to select multiple categories or tags), then click the + sign to add the term, category, or tag to the search.
To remove any term, category, or tag from the search, click the small (X) symbol that's next to the word.
On the far right, you will have a "<--CLEAR" button that will clear that row of options. For example, if you had several search terms added and were in the middle of typing something else, but changed your mind, you can click that row's "clear" button to remove ALL terms and clear the term entry box. Note that this will only affect the current row (either terms, categories, OR tags).
The other control that you'll see is the "All", "Any", or "None". "All" means that search results must match all of your categories or terms, "Any" means search results can match any of your categories, and "None" means that search results can't include ANY of the selected categories.
Tags work exactly the same way, but don't include the "None" option.
To perform the search, just hit the "Search" Button at the bottom. The page will refresh with the results below the selection box.
Also, if you hover over the date of any post, a small pop-up block will appear listing all categories and tags for the given post. The main reason I added this as a diagnostic so I could make sure that everything was working properly, but it seemed useful to keep around so use it if you wish, ignore it if you don't
See it in action
To see how it works in a live operation, just click the Advanced Blog Search to the right under the normal search box (or just click the link in this sentence).
Code
Here's what you need to make it work: DOWNLOAD
Now a break-down of the files:
jsearch_functions.php – Contains a few helper functions used by JsearchIt.php. The only thing you need to do with this is put it in the same folder as jsearch.php.
jsearch_style.css – The styles for the Jsearch box. You can change this how you wish, but put the file in the same folder as jsearch.php.
jsearch_x.gif – This is just a graphic. Put it in your theme/images folder. If you don't have an images folder in your template directory, just make one and put jsearch_x.gif in it.
positioning.js – This is needed for the pop-up function. It's not strictly necessary, but will cause a JS error if it's not there. Put it in your template folder and add the following line to your header.php file somewhere in the <head> area:
So in other words, just make sure all these files are in your theme folder (except the image which goes in the "images" folder) and then make a page that uses the jsearch template and you're set!













Ping/Trackbacks
September 16th, 2008 at 11:39 pm
Hello,
Im playing around with the Advanced Blog search. Its exactly what I need for my site, but I cant seem to size the entire box. It stretches across my page in IE and Firefox, knockin out the sidebar. Ive had a look in the jsearch_style.css but cant seem to find how to shrink it. Id like to make it about the size of your screenshot above.
Thanks for your help
September 18th, 2008 at 10:34 am
The outer table’s id is jsearch_box. So in your stylesheet you’d do this:
#jsearch_box { width: 400px}
Or similar.
September 19th, 2008 at 11:19 pm
Fantastic. Worked perfectly.
Thanks
December 4th, 2008 at 6:25 pm
This is brilliant, just the kind or thing I need after a few modifications. Thanks very much for sharing it.
Now I am wondering if there is any way it could be made work in widget form (obviously the layout would need to change drastically)?
The reason I asking this is because I would like to be able to see the search options again once I have gone to a post found by the script. If it were a widget that should be possible.
December 5th, 2008 at 7:09 am
A widget huh? Hmm. I’ve never really understood widgets or how they worked, but maybe I should learn. In either case, I doubt I’ll have much time to do anything major with this for a long time due to other time commitments. Probably not until Spring at the earliest.
December 28th, 2008 at 8:14 am
This is terrific, thanks!
My site has a lot of categories. Is there a way to get the box of categories to show alphabetically sorted by slug? It would make it much quicker for people to find the right category to select.
January 5th, 2009 at 6:34 am
Elaine, sure there is. In the jsearch.php file in the section labeled !< -- Get and print all category options -->, find the $wpdb->get_results command (it’s about 5 lines down from the heading). Replace the end of the line so that instead of this:
WHERE 1″);
You have:
WHERE 1 ORDER BY slug ASC”);
or DESC if you want a descending list.
January 6th, 2009 at 4:24 am
Fantastic, just what I needed. Thank you!
January 7th, 2009 at 3:23 am
Would it be possible to display the results of a search as excerpts from the post instead of just the 1 line link to each post?
Thanks!
January 7th, 2009 at 6:41 am
Kim, find the write_result function in jsearch_functions.php, go to the bottom just before it returns a value and replace this line:
output .= ““;
with this:
$output .= “</td></tr>”;
$output .= “<tr><td colspan=2>”;
$output .= $post->post_excerpt;
$output .= “</td></tr>”;
That should list the date and link to the article followed by the excerpt. Let me know if that works.
January 11th, 2009 at 8:50 am
Great Product! Is there anyway that a users last search will show (saved) when a user comes back to the site?
January 19th, 2009 at 6:16 pm
Hi there
This plugin is great and does what im looking for.
How ever what i would like to do is have it so that it searches the users which are registered on my site and the content which is in their profile.
Im using the profiler plugin & Register plus.
Ive created custom fields using register plus and used Profiler to display them.
The content which needs to be searched is coming from the custom fields which i have created through Register plus.
Any idea how i could achieve this ?
January 21st, 2009 at 3:44 am
Hi Jeremy,
This is a great script, exactly what I’ve been after. I tried using your previous categories script, but it didn’t work on my site (think it probably had something to do with my host, I use windows, so therefore just default permalinks as well), but this one works great.
What I really need though is the ability to manually put check boxes in that the user can select from, then query from there, so a bit more like your initial script, but hard-coded. Any chance you could give me a clue as to how to manage this using the jsearch script? I feel like I’m pretty close, but haven’t made any progress in hours.
Many thanks,
Liam
January 21st, 2009 at 9:53 am
Chad: The only way to do that is by using cookies or implementing a login for your users where you store their saved selection. Either way, It’s not functionality I plan to implement at this time.
January 22nd, 2009 at 6:14 am
Actually, never mind – I think the multiple selection drop-down menus will probably work better – just have to find a way to get that script to work on my server now
Thanks!
January 22nd, 2009 at 7:10 am
Solomon, to search custom fields, which iirc are just another data field of the post, it shouldn’t take much. You would need to add another section to the search above that made a drop-down of all current custom fields. When the data is passed to the actual search function via the “submit” button, you would again have to add code that restricts the results based on selected custom field criteria. If you understand the code I’ve provided, doing what you talked about should be pretty easy. It would just take a little time.
January 30th, 2009 at 3:58 am
Hello Jeremy,
First of all, thanks for sharing this functionality with us. It came – very much – in handy for an internal project we’re setting up here to search through an internal database of articles.
Next step for us would be to make the “term” search compatible with the “Search everything” plugin (http://dancameron.org/wordpress/wordpress-plugins/search-everything-wordpress-plugin) by Dan Cameron. We need this because we want it to search into the custom fields too by default.
Any ideas on getting us there would me mostly appreciated.
Thanks again.
January 30th, 2009 at 6:48 am
You’re the second person to mention that so I’ll probably have too look at custom fields soon. And by soon, I mean sometime in the next forever if I eventually manage to have some free time
February 4th, 2009 at 7:11 am
Hi Jeremy, I am totally speechless about your script. It is actually the thing, I’m missing for ages.
What do you think about adding the possibility to filter for a certain time range? That would be a very nice bonus feature.
February 4th, 2009 at 8:11 am
Time range huh? That’s a good idea, but I’m not certain how soon I could get that worked in… Check back in a while and maybe I can throw something together (by a while, I mean in a few weeks or so).
February 4th, 2009 at 8:30 am
Fair enough; don’t want to hurry you. I missed that functionality so long, I think I can wait a few weeks.
Another thing came up when I just played around with the script: It seems that the number of search results is limited to 10. Stupid me searched the code files for “10″ and didn’t find an expression. Any idea to solve that and change the amount of the results?
February 4th, 2009 at 9:32 am
That I can help with now. Find this option in the WordPress setting: “Blog pages show at most”. Change the value from 10 to anything else you want
February 4th, 2009 at 10:20 am
That did it! Thanks a lot!
February 25th, 2009 at 8:26 am
Hi Jeremy,
It s a really great script!
I would like that the form always be directly accessible, for example, i would like to put it in my header or in my sidebar and the result would be displayed in the main frame (exactly like the ‘normal’ wordpress search actually). How can i do this ?
Do you plan to do a widget ?
thanks a lot,
Best regards
February 27th, 2009 at 5:29 am
People have been asking about widgets a lot so I’ve been researching them a bit. I’ll update this page when I have something new (no promises, but I’m hoping sometime before March’s end)
March 3rd, 2009 at 7:02 am
Good news, I have something in the works for widgetizing this page. More info soon.
March 13th, 2009 at 1:05 pm
Great plugin, thank you so much for sharing it.
A question; you retrieve the list of all categories. Is there a way to exclude certain categories from the choice array or to sort it with subcategories?
March 16th, 2009 at 5:21 am
Bos: My new iteration of this search is in widget form and sorts the categories with their subcategories, but I’ve had people ask for alphabetical too… Maybe I’ll put a switch in so you can choose which way you want it. Sheesh this plugin is getting big.
Anyway, I’ll be posting the updated plugin soon.
March 18th, 2009 at 1:24 am
Thank you! Good luck!!!
March 18th, 2009 at 11:03 am
This is just what I’ve been looking for but it gives an error:
WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 1 for query SELECT * FROM wp_post_sorting WHERE category = made by require, require_once, include, query_posts, WP_Query->query, WP_Query->get_posts, apply_filters, call_user_func_array, post_sorting
Do you know what is causing this?
March 18th, 2009 at 7:17 pm
Also I would like to order the blog search results by title. I couldn’t figure out where to add the . ‘&orderby=title&order=asc’
March 28th, 2009 at 6:40 am
when I hit “+” no category/tag/term is added to the box
Where I made mistake? Thank you in advance.
http://shop.afas.sk/vyhledavaniiiii
March 30th, 2009 at 3:49 am
Hi Jeremy!
Nice work on JSearch.
Beside the user input for search variables, sometimes I want to bypass the Form where you create your search. I want to make pages (with JSearch template) where I only show the results with fixed predefined variables. Can you give me a tip on how I can pass this attributes to this page?
Greetings from Holland.
Dre.
April 1st, 2009 at 5:20 pm
For now, I’m not doing any new development on this until I finish the widgetized plugin. If you try it and it doesn’t work the way you want, let me know.
April 2nd, 2009 at 8:29 am
Jeremy,
one more idea for your upcoming version, which sounds very promising and it has to do with my problem above:
As you said, the amount of search or filter results depends on the settings for the “post per page”. It is quite difficult if I want to show ALL possible results but don’t want a website, that’s three miles long.
How about a “next” and “preview” link (as in “show next 10 results”)?
April 17th, 2009 at 5:58 am
I plan to have something like that. It’s getting there, but family and work demands have me working like a stampede of turtles running down a street made of peanut butter.
April 25th, 2009 at 4:38 pm
For anyone who’s been waiting, the widgetized and improved version of this search is available for testing at http://www.jordans-town.com. It’s currently experiencing some problems with pagination and correctly combining all the various filters, but I plan to work those kinks out in the next week or two.
April 29th, 2009 at 7:04 am
Jeremy,
The widget looks great. I look forward to see the final version!
May 6th, 2009 at 3:58 am
Hi Jeremy, im using your plugin on a site but cannot for the life of me get pagination working. at the moment i have to set the number of posts to display to be 9999, which for obvious reasons isnt going to scale at all well. Im using the older (original) version of the jsearch function. The new one looks pretty all emcompassing, however i really just need the ability to paginate the jsearch results within the old function and its perfect for my needs. Any chance you would be able to give us lesser coders a bone and let us know how to paginate propperly?
Thanks man, btw, great plugin.
May 6th, 2009 at 4:24 am
I can look at if you want, but have you seen the new one yet? It’s got some issues, but it’s a far more advanced and functional version of what you see above. It’s here:
http://www.jordans-town.com/jsearch
But as for your original question, I use the “query_posts” function of WordPress. I checked the code and I’m using this to preserve pagination:
// We have to preserve paging information.
if ($_REQUEST['paged'])
$jsearch_query['paged'] = $_REQUEST['paged']; // If it’s there just add it to the query
According to the post here: http://codex.wordpress.org/Template_Tags/query_posts
That should be correct. I’m not sure what the issue is, but like I said, I’m working pretty hard on the widget version right now. If you don’t think the new one will work for you, I can look at this older one, but not right away.
May 27th, 2009 at 9:41 pm
It’s really a great function!!
Thanks a lot~
I saw you can sort categories by slug.
Can I also sort the order of tags by slug?
May 29th, 2009 at 3:36 pm
Thanks. I think I’ve already done that in the new version. I’m not sure how soon the plugin will be complete however.
July 7th, 2009 at 3:15 pm
Looking forward to testing your widgetized version!
July 17th, 2009 at 12:04 pm
I just found jsearch and it is exactly what I needed. I have two clients who are going to fall in love with me all over again.
I am however having one problem. My categories (other than uncategorized) are not showing up in the category box.
Also, if I remove the code to search via tags and terms will the catgories section still work? I don’t need those and they will confuse the users of this particular site.
You can see the search box here: http://hspworldnetwork.com/search-directory
And unless I change it before you get there the page looks awful (sidebars out of whack among other things).
Jodie
http://www.outsourcingpro.biz
July 17th, 2009 at 1:42 pm
I figured out my problem… I did not have posts to the other categories. Thanks again for such an awesome bit of code.
Jodie
http://www.outsourcingpro.biz
July 20th, 2009 at 5:26 am
Hi Jeremy and thanks for an outstanding search plugin
My question is, is there a way to make a separate search result page?
What I mean with this is; When you click search, you get a search result page without the jsearch elements abow. Something similar too the standard wordpress serach result page.
Hope you can help me
Best wishes
July 21st, 2009 at 4:46 am
Knut, there are two ways to do what you’re saying.
One is to just put “the loop” above the jsearch code so it shows first and the jsearch box at the bottom.
The other is to set a variable like “$dont_show = 1″ up where the code that handles the jsearch box results is so when a search is being performed you can hide the jsearch box by testing the variable you set before. If you don’t know how to program, what I said probably won’t make any sense at all. But if you do or someone near you does, it should be a very simple fix.
July 21st, 2009 at 6:25 am
Hi Jeremy and thanks for the quick respond
I added “the loop” above the jsearch code, so that works well.
I don’t have that much experience with PHP programming, so I don’t know how to build a “$dont_show = 1″ varibale
I tried to google “$dont_show = 1″, to find something about this, but no such luck…
I would really like to have a solution that hides the jserach box when clicking search.
And unfortunately I don’t know any PHP programmers.
Is it to much to ask for your help with this?
Best wishes
Knut
July 26th, 2009 at 9:27 pm
hi jeremy, thank’s for your very great job… is there any way to put only certain category in the list ?… i look your code but i’am not an expert perhaps it depend of your line 311 of jsearch.php with the SELECT param ?
Thank’s a lot
horla
July 28th, 2009 at 4:51 am
hi Jeremy,
firts thank’s you for your great work.
I search how to choose only few categories and not all categories, i look for in jsearch.php line 304 but i’am not an expert and i don’t know how to list only somme categorie and not all…
thank’s a lot
horla
August 6th, 2009 at 5:27 pm
Horla, instead, it would be easier to not add categories to the dropdown box that you don’t want. On line 319, there’s this comment:
// Add it to the list of categories
Just after it, add this IF statement:
if (strpos(‘,slug1,slug2,slug3,slug4,’,$catortag->slug))
{
(the four or five lines that add categories)
}
What you’re doing is surrounding the lines that add a category to the dropbox with an if statement that only triggers if the category being tested is in your list. You will have to change ‘,slug1,slug2,’ etc to the slugs of your categories. Don’t forget the comma at the front and back of the string and between each slug.
August 11th, 2009 at 3:27 pm
Is it possible to have the categories show up in parent/child order? I have a site in which users need to search by country and state. Right now I am getting jumbles up list rather than having states/provinces listed under each country.
http://www.outsourcingpro.biz
September 21st, 2009 at 10:57 pm
Hey Jeremy,
What a BRILLIANT script! I can already see i’m going to get a lot of use out of it.
One question though — Are there any plans to get this working with WordPress’ new custom taxonomies?
September 22nd, 2009 at 5:11 am
Hi Jeremy,
Thank you for your time in writing this script, it solves a great problem.
Like some of the others I’m trying to exclude some categories from the selection list, I’ve tried the code above and it comes up with an error Warning: Wrong parameter count for strpos()
Do you have any ides what the issue might be.
Cheers
September 23rd, 2009 at 12:37 am
Hi Jeremy,
I’d like to know if it would be possible to implement this function in my sidebar and only show the subcategories of the category listing the user is on?
My case is, that I’m making a portfolio page, and I have sorted every page into main categories, and I’d like to have the sidebar running an ajax driven filter,
to filter the different sub categories. I’m still a newcomer, so still haven’t grasped ajax completely, so I hope you’ll be able to help me out here
kudos to you for your commitment
-Magnus
September 23rd, 2009 at 5:45 am
Allen, Custom Taxonomies? I’m not familiar with those. Can you send a link? I can say that I’m planning on major revision to improve the interface and usability however.
Edit: Nevermind, I found them myself. I think there’s a way to do that, but it’s going to take some effort
I hope I can devote some time to finishing my wigdet version of the search soon. I can look at custom taxonomies at that time.
November 18th, 2009 at 1:47 am
Yay Jeremy!
November 18th, 2009 at 1:48 am
Jeremy, when I try to add the categories using the [+] button, nothing happens… any ideas?
November 19th, 2009 at 11:20 am
It’s most likely a javascript error. It should work as-is, but I haven’t looked at the code in a while. I’m in the process of improving the search function by combining all the different search hacks I’ve done to WordPress. I’m hoping to have it done by the end of the year at which time I’ll post the updated code on all my search pages.
November 19th, 2009 at 10:17 pm
Hi Jeremy
I’ve just had a look at your Advanced Widget search function and it looks nearly exactly what I’m hoping to implement on my site.
Basically I’m hoping to create a searchable database of Businesses that are Accredited through my work.
I want to filter searches by Regions first (categories) and then Type of businesses second (tags). The word filter will also be helpful.
Is the widget ready to be downloaded and customised at all? I’m happy to report back with any problems I encounter as I go!
November 20th, 2009 at 9:27 am
Just like I was telling Angela, I’m working on a revision to this search function that combines everything I’ve learned so far with the 4 or so different search hacks I’ve written. This one will be a plugin that is widget compatible and will be more stable than my last since I’m taking the time to learn Jquery at the same time. I hope to have it done by the end of the year (one more month)
If I understand right and you’re offering to beta-test, it’s sadly not in shape yet. I’ve finished most of the functionality for the keyword search and categories, but need to fix up the tags and then the search result function.
November 22nd, 2009 at 4:33 pm
Ah well, no harm in asking eh.
I’ll keep checking back to see how it’s coming along. Seems like the exact functionality I’ll need.
If you do need a beta tester please feel free to email me (I’m assuming you can read my email address from this forum).
December 4th, 2009 at 5:46 am
I was so certain I was going to have it done this last week and it was… until I tried it in IE (UGH!). I’ve spent the last week working on making sure that the code functions correctly in every major browser (even Internet Explorer) but I’m hopeful it will be soon
December 9th, 2009 at 4:56 am
Hi,
First I must say; Nice script, just what I’m looking for
I have a question (challenge) for you:
Is it possible to split the category selection? What I mean is; You have a parent-category selection and a child-category selection.
For example, Parent is Europa and child is Norway, Sweden, Denmark…
And if you have multiply parent-categories with different child’s, is it possible to make the selections dynamical? If you choose parent 1, the content of the child selection will differ from parent 2?
I hope you understand my question (challenge)
Best wishes for the holidays
Knut
December 11th, 2009 at 10:29 am
Knut: Check out the revised version of the search (vastly upgraded) here: http://www.thegeekprofessor.com/wordpress-hacks/jsearch-plugin/
Second, I believe what you’re asking for is something I did for someone else here:
http://www.jeremyduffy.com/computers-internet/wordpress-stuff/subcategory-search-for-wordpress
I haven’t messed with it in a LONG time so I don’t know if it still works well or not.
December 18th, 2009 at 2:27 am
It’s me again
Thanks for the respond, I’ll have a look at the subcategory-search-for-wordpress
But I have another question for you… Is it possible to have the categories and tags shown as a select list (drop-down list)?
Thanks again
Knut
December 18th, 2009 at 12:03 pm
If you’re talking about the advanced search here or the new one even, all you have to do is change the
March 22nd, 2010 at 6:47 am
Hi (again),
I’m getting a script error message and I can’t figure it out:
Error message:
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.30729; .NET CLR 3.0.30729)
Timestamp: Mon, 22 Mar 2010 13:44:37 UTC
Message: Object required
Line: 357
Char: 3
Code: 0
URI: http://utestedsguiden.no/
It refers to:
function addJsearchTerms()
{
term = document.getElementById(‘jsearch_terms_input’).value; <—– This line
Do you know what this is?
Thanks for helping me!
Regards
Knut
March 22nd, 2010 at 9:15 am
Knut, this code is grossly outdated. I have the updated version here:
http://www.thegeekprofessor.com/wordpress-hacks/jsearch-plugin/
March 23rd, 2010 at 4:42 am
Hi,
I’ve looked at the new Jsearch, but unfortunately it’s to “much”. The old version that I’m using is just what I need
If I can ask you to have a look at the site I’ve used Jsearch on: http://utestedsguiden.no/
You’ll see how much I’ve modified your Jsearch script/function.
I will be so grateful if you would have a look, and help me solve that javascript error
Regards
Knut
March 28th, 2010 at 5:05 am
Yeah, I see. Ok, try this… replace the section
addJsearchTerms();
addJsearchSelectItems(‘cats’);
addJsearchSelectItems(‘tags’);
with:
$(document).ready(function() {
addJsearchTerms();
addJsearchSelectItems(‘cats’);
addJsearchSelectItems(‘tags’);
});