Search by custom field
-
In the back-end of a custom post type (post_type=inventory) I’d like the search to also search for a custom field… not it searches the title which is fine, I found the following code but when I add it, it only searches by the stock # it doesn’t search the title anymore… how to improve it? I want to keep the default search and also add the extra custom field in the query.
function custom_search_query( $query ) { $searchterm = $query->query_vars['s']; $query->query_vars['s'] = ""; if ($searchterm != "") { $meta_query = array('relation' => 'OR'); array_push($meta_query, array( 'key' => 'stock', 'value' => $searchterm, 'compare' => 'LIKE' )); $query->set("meta_query", $meta_query); }; } add_filter( "pre_get_posts", "custom_search_query");
Viewing 10 replies - 1 through 10 (of 10 total)
Viewing 10 replies - 1 through 10 (of 10 total)
- The topic ‘Search by custom field’ is closed to new replies.