Enable Custom Field Search In Wordpress

25th January 2008

When you write a post in Wordpress you can set certain custom fields. The default search behaviour of Wordpress is to search only the title and main text of the posts, which makes these custom fields not all that useful. With a little bit of tinkering you can get Wordpress to search any custom fields that you have set, so if you store things like "Author" you can allow people to view all posts by that author by clicking on a link or doing a search. To see more information about Wordpress custom fields see this Wordpress codex site article. Wordpress stores these custom fields in a table called postmeta where each custom field name (called meta_key) is associated with a custom field value (called meta_value).

Wordpress works by only accepting certain known parameters like s for searching and p for viewing pages. In order to get Wordpress to accept new search parameters and then include these as part of a search you will have to edit the core Wordpress files. The two main files you need are classes.php and query.php in the wp-includes folder. There are no other files to edit so you only need to look at these ones. The more "tinkering" you do with the core Wordpress files, the more difficult it is to update the system when new versions come out so I will keep this editing to a minimum.

First off, find the following line in classes.php, it should be very near the top.

var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots');

This is a list of the known variables that Wordpress will accept as either a GET or a POST request. In this example I will be calling the variable meta_key, to fit in with the name of the value in the table. So to pass the variable meta_key you need to add it to this list of parameters.

var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'meta_key');

That's it for classes.php, you can now close it and open up query.php. We still need to get Wordpress to accept our custom variable as part of a search so find the function called fill_query_vars(), around line 380. This function starts with the definition of an array, add our variable name to the end of this array in the same way as we did with in the classes.php file.

  1. function fill_query_vars($array){
  2. $keys = array(
  3. 'error'
  4. , 'm'
  5. , 'p'
  6. // code removed for brevity...
  7. , 'preview'
  8. , 'meta_key'
  9. );

Next, you will have to change the search function to allow you to construct the new search string. Find the line that starts with the following code, on around line 900.

  1. // If a search pattern is specified, load the posts that match
  2. if ( !empty($q['s']) ) {

Comment out this if statement and paste the following code underneath it.

  1. // If a search pattern is specified, load the posts that match
  2. if ( !empty($q['s']) ) {
  3. // added slashes screw with quote grouping when done early, so done later
  4. $q['s'] = stripslashes($q['s']);
  5. if ($q['sentence']) {
  6. $q['search_terms'] = array($q['s']);
  7. }
  8. else {
  9. preg_match_all('/".*?("|$)|((?&lt=[\\s",+])|^)[^\\s",+]+/', $q[s], $matches);
  10. $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
  11. }
  12. $n = ($q['exact']) ? '' : '%';
  13. $searchand = '';
  14.  
  15. if($q['meta_key']){
  16. $q['meta_key'] = stripslashes($q['meta_key']);
  17. $join = " INNER JOIN ".$wpdb->postmeta." ON ".$wpdb->posts.".ID = ".$wpdb->postmeta.".post_id " ;
  18. $distinct = 'DISTINCT';
  19. foreach((array)$q['search_terms'] as $term) {
  20. $term = addslashes_gpc($term);
  21. $search .= "{$searchand}((post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}') OR (meta_value LIKE '{$n}{$term}{$n}'))";
  22. $searchand = ' AND ';
  23. }
  24. $term = addslashes_gpc($q['s']);
  25. if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] ){
  26. $search .= " OR (post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}') OR (meta_value LIKE '{$n}{$term}{$n}')";
  27. }
  28. $search .= " AND meta_key = '".$q['meta_key']."'";
  29. }else{
  30. foreach((array)$q['search_terms'] as $term) {
  31. $term = addslashes_gpc($term);
  32. $search .= "{$searchand}((post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}'))";
  33. $searchand = ' AND ';
  34. }
  35. $term = addslashes_gpc($q['s']);
  36. if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
  37. $search .= " OR (post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}')";
  38. }
  39. if ( !empty($search) )
  40. $search = " AND ({$search}) ";
  41. }

This does a full text search on the post title, content and custom fields, which can lead to some odd results. To make Wordpress search for only the post title and custom fields you can take out the post_content section of the SQl string like this.

  1. foreach((array)$q['search_terms'] as $term) {
  2. $term = addslashes_gpc($term);
  3. $search .= "{$searchand}((post_title LIKE '{$n}{$term}{$n}') OR (meta_value LIKE '{$n}{$term}{$n}'))";
  4. $searchand = ' AND ';
  5. }
  6. $term = addslashes_gpc($q['s']);
  7. if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] ){
  8. $search .= " OR (post_title LIKE '{$n}{$term}{$n}') OR (meta_value LIKE '{$n}{$term}{$n}')";
  9. }
You can now close the query.php file as we will not need it from here. In order to allow you to actualy use what we have just done you need to alter the template files. If your template doesn't have a functions.php file then create one. This file contains functions that will be used with the template. Add the following code to this file.
  1. function get_meta_search_query() {
  2. return apply_filters( 'get_meta_search_query', stripslashes( get_query_var( 'meta_key' ) ) );
  3. }
  4.  
  5. function the_meta_search_query() {
  6. echo attribute_escape( apply_filters( 'the_meta_search_query', get_meta_search_query() ) );
  7. }
  8.  
  9. function generateSelect($name,$options,$default=''){
  10. $html = '';
  11. foreach($options as $value=>$label){
  12. $html .= ''.$label.'';
  13. };
  14. $html .= '';
  15. return $html;
  16. }

Next, edit the file that contains your search form. In the default template with Wordpress 2.3 this is contained in a file called searchForm.php. It has the following footprint.

 

The custom field search will be done with a drop down box. Insert the following code in the form.

  1. 'Author','mood'=>'Mood');
  2. echo generateSelect('meta_key',$valuleArr,get_meta_search_query());
  3. ?>

You can now search by custom fields!

To allow the user to see these custom fields you can do one of two things, both of which must be done within the post loop. The first one is to just use the Wordpress function the_meta(). This will print a list of the meta tags out.

the_meta();

The second is to allow users to click on those links and search by those parameters. So if a user is viewing a post they can click on the mood (to use a silly example) and see all posts that also contain that mood.

  1. Mood:ID, "mood", true);
  2. if($moods != ""){
  3. $moods = explode(",", $moods);
  4. foreach($moods as $mood){
  5. echo "" . trim($mood) . " ";
  6. };
  7. };
  8. ?>

Of course this might be easier to do with some sort of plugin. Using the Wordpress hook init might be an option.

UPDATE

This only works for versions of Wordpress below 2.5 as the structure of the file has changed since then. You shouldn't really do any core hacks so this solution is only for illistration purposes. If you want to know to do it without hacking core then have a look at Enable Custom Field Searching With Wordpress 2.6.

Comments

Permalink
Wow!!! Good job. Could I take some of yours triks to build my own site?.

Rosina (Mon, 08/04/2008 - 00:13)

Permalink
Oh course you can, that is what they are here for!

philipnorton42 (Mon, 08/04/2008 - 08:54)

Permalink
Your site is very interesting and useful.

Rosina (Tue, 08/05/2008 - 15:31)

Add new comment

The content of this field is kept private and will not be shown publicly.