3

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)

5
  • 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. Commented Feb 9, 2012 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
    Commented Feb 10, 2012 at 1:12
  • 4
    There are ways to include plugins with theme installation, for example, github.com/thomasgriffin/TGM-Plugin-Activation
    – Wyck
    Commented Feb 10, 2012 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.
    – mor7ifer
    Commented Feb 10, 2012 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
    Commented Feb 10, 2012 at 19:41

3 Answers 3

2

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 )."%";.

2
  • 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
    Commented Nov 26, 2012 at 2:48
  • Didn't know about like_escape. I'll need to remember that. Commented Nov 26, 2012 at 5:19
0

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.

1
  • I tried Search Everything, and it did not find search content written into a Wordpress page.
    – Steve
    Commented May 15, 2017 at 3:31
0

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.

2
  • Always use prepare(), never use a fixed table prefix.
    – fuxia
    Commented Nov 26, 2012 at 1:41
  • LIKE queries need to use like_escape().
    – kaiser
    Commented Nov 26, 2012 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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