Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to know how to use the built in wordpress search to return results from all content on my site. Specifically, I want to search;

  • posts
  • pages
  • tags
  • custom post type (portfolio)
  • custom taxonomy used in custom post type (skill, client)
  • metaboxes used in custom post type (portfolio_caption, portfolio_excerpt, portfolio_credits, portfolio_links)

I would like one search form and don't need to limit the search to certain post types or categories. Just enter the search term > click search > see matches from all content on my site. Simple ;)

Update: Preferably, I would achieve this through a function or custom database query rather than using a plugin. This is because I'm developing a theme and want this functionality included out of the box rather than requiring a user to download a plugin. (title changed)

share|improve this question
4  
What is the fascination with "without a Plugin"? Downvote for this restriction, since it is essentially impossible to extend WordPress functionality without using the Plugin API. –  Chip Bennett Feb 9 '12 at 23:53
    
Apologies for not explaining myself better. Rather than saying without a plugin, I should have said, that I'd like to achieve this through a function or custom database query. The "fascination" of not using a plugin is simply that I'm developing a theme and want to include this functionality out of the box without the need for a user to download a plugin. Thanks for taking a look at my question though. –  robflate Feb 10 '12 at 1:12
4  
There are ways to include plugins with theme installation, for example, github.com/thomasgriffin/TGM-Plugin-Activation –  Wyck Feb 10 '12 at 6:31
    
More importantly: anything you can do with a plugin, you can do out of functions.php...so it's really 6 of one or half a dozen of another. –  m0r7if3r Feb 10 '12 at 14:37
    
Thanks for the info. I feel this question has become a plugin vs no plugin debate. That's my fault for making the original stipulation, sorry. Does anyone have any insight into how I can actually achieve the search outlined in the question? Thanks again. –  robflate Feb 10 '12 at 19:41
add comment

3 Answers 3

up vote 2 down vote accepted

DeluxeBlogTips.com 1) has an article on how to do combined searches in both posts and meta data. Basically, it involves two queries via the $wpdb object; one to search the meta table to get a list of post_ids and the other is a query of posts to get post_ids. You then merge the arrays and use that to do a query with a WP_Query using the posts__in argument.

Using tags may be a bit tricky because 1. tags are meant to group posts together and 2. tags and taxonomies involve 3 different tables.

1) The linked article is not completely right. It should be $keyword = "%".like_escape( $keyword )."%";.

share|improve this answer
1  
+1, but an additional note: Every time you try to involve taxonomy taxons (or even their meta), you are adding one additional (and slow) JOIN to the SQL query. This can be very time consuming. –  kaiser Nov 26 '12 at 2:48
    
Didn't know about like_escape. I'll need to remember that. –  Manny Fleurmond Nov 26 '12 at 5:19
add comment

Using the link @Wyck posted, check out http://wordpress.org/extend/plugins/search-everything/. Given the complexity of the task, a plugin really is your best bet to get 'er done.

share|improve this answer
add comment

If you are truly opposed to a plugin, and know some SQL, you can do this with the $wpdb global variable.

For example, to query all posts that contain "sample_text" in the title, you would do something like:

global $wpdb;    
$post = $wpdb->get_results("SELECT * FROM $wpdb->wp_posts WHERE post_title LIKE '%sample_text%' ");

Then you would do similar things for each of the other tables.

share|improve this answer
    
Always use prepare(), never use a fixed table prefix. –  toscho Nov 26 '12 at 1:41
    
LIKE queries need to use like_escape(). –  kaiser Nov 26 '12 at 2:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.