WP plugin: Search Custom Fields

posted on April 21, 2006

Search Custom Fields is not my prettiest code, but it’s a relatively small WordPress 2 plugin doing two things at once. First and foremost it adds missing functionality for searching your post custom field or meta data. It also lets you use a separate template in your theme to display custom field search results.

Sound cool?

To install the plugin, download search-custom.zip, extract (that is, unzip) search-custom.php from it, upload the php file to your wp-content/plugins/ directory in your WordPress installation, and activate Search Custom Fields under Plugins.

Search Custom Fields does not work right out of the box. This initial release (R1.beta1) is based on a few assumptions. The biggest: users would not (necessarily) desire to have the default search pull up matches on custom fields. Instead it’s built to work around a new query string (key) passed with a search. Normal searches in WordPress work along the lines of a query of ?s=SEARCHTERM being passed to the blog’s url. For example:

/?s=plugin

lists all posts on guff which include the word ‘plugin’ in the post content. For a search of custom fields, we add key to the search query string:

/?s=plugin&key=wp

The above lists posts on guff which only have custom fields with a key of wp and value of plugin (these are posts which discuss each of my WordPress plugins). Again, it does not search post content AND custom field values. It searches only on custom fields; the key query string is the…key here. “But how do I automatically add this ‘key‘ to searches?” you may ask. No, go ahead, ask. Well, there’re several ways to go about it, depending on how you want people to search your custom fields. But all require a modification to the search form in your Theme template(s), or perhaps a new form specific to your custom field search. First, let’s look at the typical WordPress search form:

<form method="get" id="searchform" action="/">
<input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</form>

Yours may differ in specifics, but all we need here is a framework to display examples. For example…

If you only want to search on a single key, the easiest method to provide custom field searching is to add a hidden input type to the form:

<form method="get" id="searchform" action="/">
<input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="hidden" name="key" value="mood" />
<input type="submit" id="searchsubmit" value="Search" />
</form>

Notice the hidden input (which as one guesses by the type, doesn’t display) has a name attribute of “key” and value of “mood.” This now searches only on ‘mood’ custom field entries. Great for discovering when you were sad, happy, annoyed, and so on. Ok, perhaps you don’t want to know this, but this method can be used for a custom field search where you want to define just one key as the search parameter.

What if you want a selection of custom field search keys? The radio button or input type comes in handy for this:

<form method="get" id="searchform" action="/">
<label for="author">Search Books: <input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" /></label>
<input type="submit" id="searchsubmit" value="Search Books" /><br />
<label for="author">Search by Author: <input name="key" type="radio" value="author" /></label>
<label for="title">Search by Title: <input name="key" type="radio" value="title" /></label>
<label for="isbn">Search by ISBN: <input name="key" type="radio" value="isbn" /></label>
</form>

Another option is the select element, where you can provide a dropdown list of keys to choose from:

<form method="get" id="searchform" action="/">
<input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<select name="key">
<option>record</option>
<option>book</option>
<option>film</option>
<option>show</option>
</select>
<input type="submit" id="searchsubmit" value="Search" />
</form>

Limitations to search in this version (R1.beta1) are:

  • As noted, a standard search still ignores custom fields. This restriction is by design, which can be modified based on what feedback I get.
  • You can only search on one key at a time. If the interest is there I’ll look at providing for multiple key searches.
  • A standard post search occurs if the key query is empty. Not sure if this is the expected result for most, but that’s why it’s a beta.

About that other thing the plugin does

You’d think I forgot!

For those who would like a different design or layout with their custom field search results (different from the standard search results on your blog, that is), I’ve added functionality for that in the way of an alternative template. If you place a search-custom.php file in your active theme’s directory (not the plugin file search-custom.php—a template for your theme which you name “search-custom.php”), WordPress will use this for custom field search results instead of search.php, or the default template. This is actually the cool part for me…

Author: Kaf Oseo
Categories: WordPress
Comments: (33) · Leave a comment · Comments RSS2

Christopher
Comment » May 19, 2006 @ 2:25 am

I really like this plugin, and appreciate you making it available for use. I am really interested in either searching more than one custom field, or combining the meta search with the standard search (either will accomplish what I’m looking for) – Any help or tips for getting up and running with this functionality would be great! Thanks Again.

Scott Plumlee
Comment » July 24, 2006 @ 11:18 am

I’d be interested in being able to do multiple keywords, as well as keyword+category (think of a price search on all items in Category X), and of course also being able to search post content plus keywords.

Scott Plumlee
Comment » July 24, 2006 @ 1:54 pm

I’d also like to see (if you want to do any further development, not asking/demanding) an option to search exactly on a value or with wildcard matching. IE, an option to make the search query ‘LIKE $search’ or ‘LIKE %$search%’. I’m using it to search values that represent page in a catalog, so I only want exact results. I’m making a change to the code to make it work for me, but just a suggestion.

Scott Plumlee
Comment » July 24, 2006 @ 2:40 pm

Ok, I’m going to give one more suggestion – please don’t think I’m not appreciative of the plugin, though. Give an option to order the results either alphabetically (maybe make that a default?) or by some other option. Although I think this might be a code choice in the actual search code itself, not your plugin.

Rus
Comment » July 25, 2006 @ 12:55 pm

I would like to be able to search ALL custom fields to return results. Wildcard matching is good, multiple keywords, etc.I am building a site that pulls different content out to make the post depending on what page you’re in. The search results will group multiple posts together if they have the same origin. For instance – think about five different posts, one of the fields is “store” – two of the posts are from the same store – they get merged into one “post” if you search right.

Thanks!

Brent
Comment » August 23, 2006 @ 10:32 pm

This may be a dumb question, but I’m relatively new to WordPress, will this work on Pages as well as Posts? Are most of the WordPress commands that are used to pull in Posts also used to pull in Pages?

Jorux
Comment » November 20, 2006 @ 6:53 am

I really appreciate the plugin which can search metadata.
I am a Chinese medical university student, and web design is my biggest hobby. But I have no idea how to deal with php/mysql.
Could you develop the plugin more deeper to make it search multiple key one time.
And I am to pay for the plugin.
Could you reply this to my email?

Masoud
Comment » December 31, 2006 @ 4:35 pm

Hi,
I have a question about mysql query.
how can i put the one of the result file in a variable?
$show= “Select * form members where Username=’Mike’”;
$result=mysql->query($show) ;
$Name=$resul->Name;
it dose not work?
what can i do?
mer30

Ricardo Peirera
Comment » February 5, 2007 @ 2:22 pm

Hi!
Isn’t something missing?
After adding some code I get the following error:

[Unknown table 'wp_postmeta' in where clause]
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE blah,blah,blah

Is there any easy way to force ‘FROM wp_posts,wp_postmeta‘ in the SELECT clause(s)?

Thanks in advance!

UPDATE: Opssie, found the problem! Had to update the szub_is_search_key() function (‘s’ could be empty in my searches).

Tim
Comment » March 16, 2007 @ 9:39 pm

Hi, thanks for the plugin,,, I would really like to use it for multiple key searches, any plans to develop this?

Yo
Comment » March 19, 2007 @ 10:40 pm

I would like to search in more than one custom field!

Martins
Comment » May 14, 2007 @ 9:40 am

Thanks. I would also like to search in more than one custom fields. Maybe this search could be merged with http://www.zirona.com/software/wordpress-advanced-search/ ?

Martins
Comment » May 16, 2007 @ 3:36 pm

Hi, I updated your plug-in by adding auto HTML form generated from the existing custom field values as drop-down lists and by adding a search across all the custom fields. http://kamvajag.lv/programming/custom-search/.

Jane
Comment » May 27, 2007 @ 1:00 am

Hello there. I am not sure whether you are still looking at feedback for this plugin, but I would love to see it integrated with custom fields. But more importantly, when I change the user variable to one, I get an error message which may conflict with UTW tags:

WordPress database error: [Unknown table 'wp_tags' in where clause]
SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.* FROM wp_posts LEFT JOIN wp_post2cat ON ID = post_id LEFT JOIN wp_categories ON cat_ID = category_id WHERE 1=1 AND (((post_title LIKE ‘%brava%’) OR (post_content LIKE ‘%brava%’)) AND ((post_title LIKE ‘%and%’) OR (post_content LIKE ‘%and%’)) AND ((post_title LIKE ‘%june%’) OR (post_content LIKE ‘%june%’)) OR (post_title LIKE ‘%brava and june%’) OR (post_content LIKE ‘%brava and june%’)) AND (post_type = ‘post’ AND (post_status = ‘publish’ OR post_status = ‘private’)) OR wp_tags.tag like ‘%brava and june%’ OR cat_name LIKE ‘%brava and june%’ AND post_type = ‘post’ AND post_status = ‘publish’ GROUP BY ID ORDER BY post_date DESC LIMIT 0, 10

Travis Dahl
Comment » June 1, 2007 @ 3:32 pm

Hey Kaf,
I have kind of an odd request/question. Is there a way I can have a default custom field with a key of “name” on every post with a default value of the logged in users first and last name? This way I could use your plugin to search for posts by authors.

On second thought 2 would be better, one for real name (first and last name) and one for username.

Travis Dahl
Comment » June 19, 2007 @ 1:37 pm

Thank you Lester! (the answer to my previous post)

http://wordpress.org/support/topic/121766?replies=2#post-577154

SAID MOHAMED
Comment » August 13, 2007 @ 2:33 am

I HAVE A PROBLEM WITH MY SEARCH QUERY WHICH I HAVE CREATED TO SEARCH USING MULTIPLE FIELDS,
CAN U GIVE ME AN EXAMPLE OF SEARCH QUERY OF THIS KIND.
THANX

boris kislitsin
Comment » August 29, 2007 @ 3:29 am

Hello,
it looks as a great plugin, but I have a problem with getting the results.
I have tried to insert different things into searchform.php, (I run WP 2.2.2. and K2 theme)but all I have is a message with mistakes, something like this:

WordPress database error: [Table 'memoryce_memoryce.wp_jkeywords' doesn't exist]
SELECT DISTINCT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts LEFT JOIN wp_post2tag p2t on wp_posts.ID = p2t.post_id LEFT JOIN wp_tags on p2t.tag_id = wp_tags.tag_id LEFT JOIN wp_comments ON ( comment_post_ID = ID ) LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id LEFT JOIN wp_jkeywords ON wp_posts.ID = wp_jkeywords.post_id WHERE 1=1 AND (((post_title LIKE ‘%kislitsin%’) OR (post_content LIKE ‘%kislitsin%’) OR (post_excerpt LIKE ‘%kislitsin%’))) AND (post_password = ” AND (post_status = ‘publish’ or post_status = ‘attachment’ OR post_status = ‘private’)) OR wp_tags.tag = ‘kislitsin’ OR wp_tags.tag like ‘%kislitsin%’ OR (comment_content LIKE ‘%kislitsin%’) OR meta_value LIKE ‘%kislitsin%’ OR tag_name LIKE ‘%kislitsin%’ GROUP BY ID ORDER BY post_date DESC LIMIT 0, 4

As you see, I’ve tried to search author’s posts by name (kislitsin).
Here is the original code:

“>
” />
” />

Can you suggest what I should put in? It could be great if I could search all my meta (like user’s name, and nickname etc) but if it would be possible just type in a name and see the results, it would be GREAT.
Thanks and the very best wishes from Bangkok,
Boris.

Richard
Comment » June 13, 2008 @ 7:14 am

I really like this plug-in and it’s so close to what I wanted. But for some reason it returns duplicate search results when the custom field data also appears in the post content. Any ideas how to stop this from happening or what I need to amend to fix this?

Richard

radeondt
Comment » June 14, 2008 @ 11:03 am

It’s an impressive plugin, but can you make the plugin that can search multiple keys, such as .. I want to search Real Estate, that sell in new york and have 4 rooms …

Thanks

Phantasmes
Comment » June 22, 2008 @ 10:09 am

Same here, is it possible?

Adam
Comment » August 14, 2008 @ 5:49 pm

I’ve noticed that when this plugin is enabled and somebody searches for a post title, the post with that title does not appear in the search results.

For example, my site is a restaurant aggregator, and when somebody searches for the name of a restaurant (which is how the posts are titled), the post doesn’t come up.

Is there any way for me to also incorporate the post title in the search?

Thank you,
Adam

jrgd
Comment » September 23, 2008 @ 8:11 am

Hi – i’m trying to get it working on wp2.6.2 but feel a bit lost
did anyone succeed? any insights/advice?
thanks in advance

Kevin
Comment » October 13, 2008 @ 9:30 pm

I think I understand the way this plugin works, but I am not sure if it can do what I want it to. I want to attach a custom field of “Location” to each post, and have that custom field print and be clickable, linking to a search of posts with the same custom field. I can display the custom field with this bit of code.

>>>>>ID, ‘custom_key’, true); ?>Key Name: <<<<<

That part is easy, but can I use this plugin to have link to search of the value of that location custom field? I know it would be easier to have my locations in my tags, but I have to have them separate. Thanks!

Adrian
Comment » October 30, 2008 @ 3:45 pm

Hi there, when Beta1 R1 is operational (the one in the zip file) it doesn’t correctly search on the field specified. R2 (the one linked as ‘source’ at top) works perfectly. But, when activated, displayed at top of every single WordPress 2.6.3 page(at the start of the html source) (and earlier versions too for me) is shown precisely: | |

I’ve attempted commenting out different functions but is has no affect. I would merge the R2 into the R1 in the zip file and if you can, trouble shoot the | | displayed at the top of every page when the R2 version is activated instantly as the first server output.

Adr\
Comment » October 30, 2008 @ 4:11 pm

Also, when deactivating R2 , the following error appears when plugin deactivated. When reloading the page, it deactivates properly. Another interesting feature would be to search on two fields for the same query.

Warning: Cannot modify header information – headers already sent by (output started at /home/launchpa/public_html/cleanzap/s/wp-content/plugins/search-custom.php:1) in /home/launchpa/public_html/cleanzap/s/wp-includes/pluggable.php on line 770

jeremy
Comment » February 4, 2009 @ 12:29 pm

i got excited when i saw this, but can’t seem to make it fly. i’ll detail it and maybe you or someone can help me out.

1. using wp2.7.
2. custom-field called “class” with values of ’5′, ’4′,’3′,’2′,’1′,’0′ representing the stars of a hotel.
3. here’s the form code:

<form method=”get” id=”searchform” action=”">

5 stars
4 stars
3 stars
2 stars
1 Star
Not Classified

i added the custom-search.php template to the theme directory. I can’t seem to return any results.

my querystring matches up according to your example after form submission as shown here “/index.php?s=4&key=class”

i copied the functional “search.php” and re-named it “custom-search.php” with some formatting adjustments. what am i missing?

veridisko
Comment » June 6, 2009 @ 4:31 pm

Same problem than yours Jeremy : I’cant make it work On WordPress 2.7.
Maybe someone could help us, I really need this custom fields seach.

Rotten Elf
Comment » January 24, 2011 @ 4:30 pm

I’m on the hunt for a solution to custom fields search. This one is apparently outdated as it’s pre-WP 3.0. Do any of you have a solution for “Custom Fields Search” for WP 3.0 and higher? I’m in dire need of a solution to this issue and have been scouring the internet for about 1.5 days now….with pretty much no luck. Any help would be greatly appreciated. I will gladly write an article, sharing the code and giving a link back to the person who has the code for a little linkjuice on one of my blogs. Thanks in advance!

RodgerFox
Comment » January 26, 2011 @ 8:17 am

Good time.
I am about your plugin.
Please tell me how to do a search as there http://www.ip4properties.com/, rather I am interested in the field:
Select state / city and Select area
Content for Select aria appears only when a Select state / city
Thank you very much, with respect Valentin.

The problem is that there are countries and regions in each country to country.
User in selecting the country can also select a region.
A good example is in the link I sent you.
I am insanely happy that you responded to my request.
I would be glad if you will send quote php code.

carly
Comment » March 7, 2011 @ 1:14 am

Hi there,

this is a great little tool, I just need to know how to make it search for 2 different words in the fields rather than looking for all the words and only showing posts that have all the words. eg. if I’m looking for fun games and I have 3 posts, 2 have the word fun and 1 has the word fun games it will only bring up this post, it won’t bring up the others.

it’s not functioning as if the OR function is working, it’s only looking for 1 single word or exact phrase. I don’t know how to fix it, does anyone?

Rotten Monkey
Comment » May 11, 2011 @ 3:57 pm

I feel that this plugin may be my answer, but with all the people here looking for support and not finding it, I feel a little concerned about using it on my site. Has anyone gotten this plugin to search multiple custom fields effectively? It’s such an awesome idea, but only worthwhile if we can get it to function accordingly…

 

* Required field (e-mail is not published). Breaks and paragraphs are automatic. HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>