1

As you know WordPress search only look at the post titles and contents, but I want the search to include also a custom field. Let me explain more:

Consider a movie post with the title "Fast Five", this post has a custom field in wp_postmeta table with the name of "release_year". I want the search to find if a user search for "Fast Five 2011". 2011 is the text which is stored in wp_postmeta table.

Any help is appreciated.

4
  • can you clarify, are you talking about including that field in search, or are you talking about searching only posts that contain that field? There are a lot of semantics around your search request that make this task extremely difficult. For example, Searching for both "Fast Five" and "2011" in a text string like your example eliminates the vast majority of solutions, and may make your request impossible without external software such as Algolia or Elastic Search. WP search is very primitive and simple, and SQL filters can help you also search for a term in post meta, but not like that
    – Tom J Nowell
    Mar 5, 2023 at 16:53
  • @TomJNowell Yes I want exactly to find posts with the title "Fast Five 2011", where the year 2011 is stored in wp_postmeta table. Do I need to seperate the year from the search string before to pass it to the query? Mar 6, 2023 at 14:54
  • yes, if you want it to find posts that do not contain 2011 in the title or content but do have it in post meta then you can't use "Fast Five 2011" as your search string. What you want is extremely difficult and may be impossible ( very high chance ), or very slow to run. This becomes very easy if you use 3rd party non-WP search engines such as Openflow or Elastic
    – Tom J Nowell
    Mar 6, 2023 at 19:39
  • also storing the release year in a post meta field and using meta_query is going to be very bad for performance when using meta_query. It would be significantly faster and scale better if release_year was a custom taxonomy, where each term was a year, and it used tax_query instead. Post meta is fast when you already know the post ID/key, it's not intended for searches or finding posts.
    – Tom J Nowell
    Mar 6, 2023 at 19:47

2 Answers 2

0

This isn't possible because your search term would not appear anywhere. You can search for the entire search term, but you can't search for one part in the title/content and another part in the meta/fields, not without separate fields and separate values.

The only way this would work is if the title had the year in it, or the content had the year in it, or the meta contained the "fast five" part too, but if you want to search across all of it with part being in the meta and the other part being in title/content, then no, you can't do that in stock vanilla WP. You would have to install 3rd party seach engine software such as OpenFlow/Algolia/Elastic Search that your host may not support.

WordPress search is very very primitive and not sophisticated. You should have very low expectations.

However

If you had asked how to search and also filter by year though, that would be much easier, e.g. if you had a search field with a dropdown for the year.

Searching for "Fast five" with a separate filter box of 2011 is a much easier problem to solve, allowing you to swap $query->query_vars['s'] in claika's answer for a GET variable for your filter input/dropdown selector.

2

Including custom fields in WordPress search can be done by modifying the search query to include the custom field data. Here's a step-by-step guide on how to achieve this:

  1. Create a custom field: First, you'll need to create a custom field in your WordPress posts. This can be done using a plugin such as Advanced Custom Fields or by manually adding the custom field to the wp_postmeta table in your database.

  2. Modify the search query: Next, you'll need to modify the search query to include the custom field data. This can be done by using the pre_get_posts action in WordPress. Here's an example of how to do this:

function include_custom_field_in_search($query) {
    if ($query->is_search) {
        $query->set('meta_query', array(
            array(
                'key' => 'release_year',
                'value' => $query->query_vars['s'],
                'compare' => 'LIKE'
            )
        ));
    }
    return $query;
}
add_action('pre_get_posts', 'include_custom_field_in_search');

In this example, we're using the pre_get_posts action to modify the search query before it is executed. The meta_query argument is set to include the custom field with the key release_year and a value that matches the search term. The compare argument is set to LIKE, which means that the search will match any posts where the custom field contains the search term.

  1. Test the search: Finally, you can test the search to see if the custom field data is being included. You should now be able to search for a movie by its title and release year, and the search results should include any posts that match the search term.

This is an example of how to include custom fields in a WordPress search. By modifying the search query to include the custom field data, you can improve the accuracy and relevance of your search results.

3
  • Thanks for your reply but when I add this code in function.php, the search is completly stopped and does not find anything even if I search without the meta data. Mar 6, 2023 at 14:36
  • This is the query reslut I get: Mar 6, 2023 at 14:49
  • this won't work with the example the OP mentioned because only part of it is in the title and only part of it is in post meta, and there's no way to know which with just a single string value
    – Tom J Nowell
    Mar 6, 2023 at 19:40

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.