Search Meta Hack
Search Meta is a WordPress 1.5 hack that makes search queries look at custom field values in addition to post content. It is compatible with my Search Pages plugin. The current version is 0.1 and was released on April 7, 2005.
Please note that this requires you to edit core WordPress files. Do so at your own risk! I will not be responsible for any damages this causes if something goes unexpectedly and horribly wrong. :)
The Search Meta hack is adapted from the old Search Meta Post Data hack for Mingus.
Comments, bug reports, and other issues can be discussed on my forums.
Download
[ patch ] [ .tar.gz ] [ .zip ]
Known Issues
There is currently a bug that will cause draft posts to show up in searches. If you typically have saved drafts, you may not want to implement this hack until I can fix that.
Installation
There are three ways you can apply this hack: replace a file, patch a file, or manually edit a file. The file that will be replaced, patched, or edited is wp-includes/classes.php.
Replacement Method
This method is probably the simplest. However, it is only advisable if you’re using WordPress 1.5 and haven’t applied any prior hacks to wp-includes/classes.php.
- Download either the .tar.gz or the .zip above.
- Extract classes.php from the archive.
- Upload the file to wp-includes.
Patch Method
This method is also fairly simple, but requires that you have the patch utility installed on your server and have shell access to run it. If you’ve applied other hacks to wp-includes/classes.php it will probably work (unless your hacks are quite extensive).
- Download the patch file above.
- Upload it to wp-includes.
- Open a shell to the server and cd to the wp-includes directory.
- Type this command (while in the wp-includes directory):
patch < search_meta-0.1.patch
Manually Edit Method
This method will take the most time, but if you run into problems with one of the other two install methods you can at least hopefully make it work this way. :) The directions apply to file wp-includes/classes.php. I’m including a lot of context so that it may make it easier for people who have a lot of other hacks (or for someone who may be trying to do this on a different version of WordPress).
- Somewhere around line 348, look for the following section of code:
[php num=348]if (!$q[’sentence’]) {
$s_array = explode(’ ‘,$q[’s’]);
$q[’search_terms’] = $s_array;
$search .= ‘((post_title LIKE ”.$n.$s_array[0].$n.”) OR (post_content LIKE ”.$n.$s_array[0].$n.”))’;
for ( $i = 1; $i < count($s_array); $i = $i + 1) {
$search .= ‘ AND ((post_title LIKE ”.$n.$s_array[$i].$n.”) OR (post_content LIKE ”.$n.$s_array[$i].$n.”))’;
}
$search .= ‘ OR (post_title LIKE ”.$n.$q[’s’].$n.”) OR (post_content LIKE ”.$n.$q[’s’].$n.”)’;
$search .= ‘)’;
} else {
$search = ‘ AND ((post_title LIKE ”.$n.$q[’s’].$n.”) OR (post_content LIKE ”.$n.$q[’s’].$n.”))’;
}[/php]
You need to add three lines as shown below right after line 356:
[php num=348]if (!$q[’sentence’]) {
$s_array = explode(’ ‘,$q[’s’]);
$q[’search_terms’] = $s_array;
$search .= ‘((post_title LIKE ”.$n.$s_array[0].$n.”) OR (post_content LIKE ”.$n.$s_array[0].$n.”))’;
for ( $i = 1; $i < count($s_array); $i = $i + 1) {
$search .= ' AND ((post_title LIKE ''.$n.$s_array[$i].$n.'') OR (post_content LIKE ''.$n.$s_array[$i].$n.''))';
}
$search .= ' OR (post_title LIKE ''.$n.$q['s'].$n.'') OR (post_content LIKE ''.$n.$q['s'].$n.'')';
$search .= ')';
for ($i = 0; $i < count($s_array); $i = $i + 1) {
$search .= ' OR ('. $wpdb->postmeta .’.post_id=’. $wpdb->posts .’.id AND ‘. $wpdb->postmeta .’.meta_value LIKE \”. $n . $s_array[$i] . $n . ‘\’)';
}
} else {
$search = ‘ AND ((post_title LIKE ”.$n.$q[’s’].$n.”) OR (post_content LIKE ”.$n.$q[’s’].$n.”))’;
}[/php] - Somewhere around line 535, look for the following section of code:
[php num=535]// Apply post-paging filters on where and join. Only plugins that
// manipulate paging queries should use these hooks.
$where = apply_filters(’posts_where_paged’, $where);
$where .= ” GROUP BY $wpdb->posts.ID”;
$join = apply_filters(’posts_join_paged’, $join);
$request = ” SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1″.$where.” ORDER BY post_” . $q[’orderby’] . ” $limits”;[/php]
Modify line 540 as shown below:
[php num=535]// Apply post-paging filters on where and join. Only plugins that
// manipulate paging queries should use these hooks.
$where = apply_filters(’posts_where_paged’, $where);
$where .= ” GROUP BY $wpdb->posts.ID”;
$join = apply_filters(’posts_join_paged’, $join);
$request = ” SELECT $distinct “.$wpdb->posts.”.* FROM $wpdb->posts, $wpdb->postmeta $join WHERE 1=1″.$where.” ORDER BY post_” . $q[’orderby’] . ” $limits”;[/php]
Credits
As mentioned above, the Search Meta hack is adapted from the old Search Meta Post Data hack for Mingus. All I did was figure out where the changes had to be made in Strayhorn and make some files for it.
Version History and Changelog
0.1a - 2006-12-02 [ no changes ]- Fixed some typoes in the manual edit version: some single quotes were not properly escaped. Thanks to Jen Gallardo for catching them!
0.1 - 2005-04-07 [ patch | .tar.gz | .zip ]- Initial release.