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: (22) · Leave a comment · Comments RSS2 · Trackback URL

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?

 

* 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>