• Hello, how to add wp_postmeta in search function? Right now I have a custom page template what saves data in postmeta, and I need to make it to work with search. Right now it wont search content of my custom page in search. Please help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter thranduil

    (@thranduil)

    Not supported for 5.0.3 version. And it would be better if I could add some new function to search , not just add random search plugin, because we have new search template made for our website too.

    If you have the post meta and your have the search template, all you need is the code that expands the search query. You don’t have to use the plugin if you think it won’t work with the latest version (it likely will), but you could look at the code to see how it’s done.

    Moderator bcworkz

    (@bcworkz)

    What to do depends on whether you want the meta results to be logically ANDed or ORed with other results. For ANDed results, you could modify search queries through the “pre_get_posts” action. Your callback would check that $query->is_search() returns true. If so, get the “s” query var to get the search terms. Use the terms to construct a “meta_query” array for WP_Query arguments, which is set as the “meta_query” query var in your callback.

    If you want OR logic (widens results instead of narrows), you’ll need to modify the actual SQL query through either “posts_where” or “posts_request” filters to change the default “AND” created by the above query var addition to “OR”. In “posts_request” filter, you could completely replace the SQL as desired without first changing query vars in “pre_get_posts”.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add wp_postmeta to search.’ is closed to new replies.