2

I can't seem to figure out how to include tags in a wordpress search. For example, if I search for 'apple', I would like to get back posts with 'apple' in the title or the content (default wordpress functionality) as well as posts that contain the tag 'apple'; When I add the 'tag' => $keyword line into WP_query then I get no results for every search.

$keyword = get_search_query();
$args = array(
    'post_type' =>  array('case_studies', 'news', 'events'),
    'post_status' => 'publish',
    's' => $keyword,
    'tag' => $keyword,
);
$query = new WP_Query($args);

If I remove it, I get results as normal (but without posts with the keyword in its tags)

1

This would only work if the keyword exactly matches the tag that you are searching for, and unfortunately, tag data isn't indexed by WordPress search by default, so that won't yield anything either. If you want to index the tags and include that in search results you might want to use a plugin. Relevanassi might do the trick, but if you don't mind spending a bit, then SearchWP would definitely do it, since it indexes all content, including tags and custom fields, and lets you assign weight to each one. You could for instance, give tags the highest weight, or if you only want to search tags, simply give everything else a weight of 0.

| improve this answer | |
  • So, am I missing something about the functionality of tags? What are they for if you cannot search for stuff with them? – myol Jan 29 '15 at 20:55
  • 1
    @myol there may be a disconnect. Tags really are a flat (meaning hierarchical, no parents and children), way of organizing content. When pulling in content on your site's front-end they can a useful thing to query against. But when accepting user input, you'll need a plugin to make sure that tag content is indexed. – Jason Hoffmann Jan 30 '15 at 21:24
1

Your problem is more than likely that you are nullifying your search results.

$keyword = get_search_query();

Above, you are grabbing the search variable provided

$args = array(
    'post_type' =>  array('case_studies', 'news', 'events'),
    'post_status' => 'publish',
    's' => $keyword,
    'tag' => $keyword,
);

Here, you are executing a query that requires ALL of the following to be true

  • The post type must be case_studies, news, or events
  • The post status must be published (publish)
  • The title (or possibly content) must contain the keyword searched on
  • The post must contain a tag named exactly as the search term

Drop the s or tag declaration and you might fare better.

Each item you enter into your WP_Query arguments list must be true for a result to be shown. ( sub queries such as tax and term queries have special syntax to do OR type queries on their data )

If you want to search just for posts having a particular tag, try the following.

$keyword = strtolower( str_replace( array(' ','_'), '-', get_search_query() ) );
$args = array(
    'post_type' =>  array('case_studies', 'news', 'events'),
    'post_status' => 'publish',
    'tag' => $keyword,
);

That will give you a match if the tag slug exactly matches the entire search.

str_to_lower is used as tag searches tag slugs, not names and tag slugs are all lower case with dashes (the reason for the str_replace, which replaces all spaces and underscores with dashes)

What are you trying to do? If you can state that clearly in your question, perhaps one of us can provide a solution to the problem for you.

| improve this answer | |
  • I have clarified my question. Part of me thinks I may have to do two separate searches and merge them together... – myol Jan 29 '15 at 21:58
  • Very possibly. Alternately, if you are familiar with SQL, you could write a custom query to grab exactly the data you want (e.g. a list of matching post ids) and then use WP_Query if you want to grab just those posts as proper objects to loop through. For complex things I tend to do that a lot. – Privateer Jan 29 '15 at 22:02
  • My knowledge of MySQL is lacking and I haven't a clue where and how WP stores the information I'm looking for. Wouldn't even mind having just the IDs returned. – myol Jan 29 '15 at 22:09
1

This is quite an unusual case an and also one that can fail on you and give quite unexpected results.

By default, this is not just possible at all. I really tried to overthink this, and I cannot come up with a solution that will work by simply using one query, except maybe by using a custom SQL query or the filters supplied in WP_Query. Unfortunately my SQL is still very bad, so I cannot help you here. You will need to look at the two links given.

You will probably need to do some check though to check whether or not that a tag does exist before actually using it. Here you can make use of term_exists to test if the tag exists.

I also thought of dividing the search page into two sections, the first section displays the posts from the default search, the main query. The second section will be a custom query which will retrieve posts from the specific tag. Here you would simply just use the tag parameter and use the search term as the value. As I stated before, this might be a good idea to first check if the tag actually exists.

The drawback here might be duplicated posts, so you will need to maybe check for this as well.

There is really a lot that you can play around with here and this is just basic ideas that I have given you. Feel free to post your solution and accept it if you are able to solve this :-)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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