I have created a custom post type and have attached some custom fields to it. Now I would like to the search that authors can perform on the custom post list screen (in the admin backend) to also be performed on the meta fields and not only look in the title and content as usual.
Where can I hook in and what code I have to use?
Example image
Stefano
I solved filtering the query by adding the join on the postmeta table and changing the where clause.
tips on filtering the WHERE clause (often require regular expression search&replace) are here on codex:
Stefano answer is great but it lacks a distinct clause :
Add the code above update it and it will work with no duplicates.
This will work,
Answer 1:
Add this code in function file, and change and add more columns name, that you have used in your custom post type
Answer 2: Recommended
Use this code in function file without any change
With this code you can search in post list in WordPress Admin Panel with custom post meta values along with title and other default fields.
Please, add below code in functions.php file:
The version of the code here in a couple answers that modifies the meta_query parameter of the search’s WP_Query in pre_get_posts was no longer searching the post_title. Adding the ability to search either the post title, OR meta values can’t be done directly in WP_Query without modifying SQL unfortunately, as this question elaborates upon: Using meta query (‘meta_query’) with a search query (‘s’)
I’ve combined some of the techniques here to get a working version that avoids preg_replaces and too much SQL modification (I wish it could be avoided entirely). The only downside being that after a search, subtitle text at the top of the page says “Search results for ””. I’ve just hidden that with CSS for my plugin’s custom post type.