WP plugin: Search Custom Fields
- plugin:
- Search Custom Fields
- version:
- R1.beta1
- download:
- search-custom.zip
- source:
- search-custom.php
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:
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:
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
keyquery 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: (27) · Leave a comment · Comments RSS2 · Trackback URL
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.
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.
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.
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.
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!
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?
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?
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
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).
Hi, thanks for the plugin,,, I would really like to use it for multiple key searches, any plans to develop this?
I would like to search in more than one custom field!
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/ ?
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/.
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
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.
Thank you Lester! (the answer to my previous post)
http://wordpress.org/support/topic/121766?replies=2#post-577154
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
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.
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
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
Same here, is it possible?
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
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
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!
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.
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