I’m developing a WP plugin which uses custom post types and meta data. What I’m trying to achieve is having the meta data searchable. Here is some code that I found which searches the meta data fine, however I can’t get it to display the search string on the CPT page.
add_filter( "pre_get_posts", "custom_search_query");
function custom_search_query( $query ) {
$custom_fields = array(
"pctracker_company",
"pctracker_customer",
"pctracker_phone1",
"pctracker_phone2",
"pctracker_mobile",
"pctracker_email"
);
$searchterm = $query->query_vars['s'];
$query->query_vars['s'] = "";
if ($searchterm != "") {
$meta_query = array('relation' => 'OR');
foreach($custom_fields as $cf){
array_push($meta_query, array(
'key' => $cf,
'value' => $searchterm,
'compare' => 'LIKE'
));
}
$query->set("meta_query", $meta_query);
};
}
I know that in the code its unsetting ‘S’, however if you don’t unset it, it does not display any results.
Can anyone point me in the right direction?
Thanks very much,
Jason
EDIT:
I’ve just been having a play around and I’ve got this working!
function my_search_results($query){
if(isset($_GET['s'])){
if($_GET['post_type'] == "pctracker_customers") {
$mySearch = $_REQUEST['s'];
$query = 'select * from wp_posts,wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = "pctracker_phone1" AND wp_postmeta.meta_value LIKE "%'.$mySearch.'%" AND(wp_posts.post_status = "publish")';
}
}
return $query;
}
add_filter( 'posts_request', 'my_search_results');
EDIT 2:
I’ve got multiple meta fields I need to search, pctracker_phone1 and pctracker_phone2 are just a couple. I’ve edited the SQL code so it searches for both. However the SQL code is going to be massive once all of the other metafields are added. Do you know how I could simplifier this:
$query = 'SELECT * FROM '. $wpdb->prefix .'posts,'. $wpdb->prefix .'postmeta WHERE '. $wpdb->prefix .'posts.ID = '. $wpdb->prefix .'postmeta.post_id AND '. $wpdb->prefix .'postmeta.meta_key = "pctracker_phone1" AND '. $wpdb->prefix .'postmeta.meta_value LIKE "%'.$mySearch.'%" OR '. $wpdb->prefix .'posts.ID = '. $wpdb->prefix .'postmeta.post_id AND '. $wpdb->prefix .'postmeta.meta_key = "pctracker_phone2" AND '. $wpdb->prefix .'postmeta.meta_value LIKE "%'.$mySearch.'%" AND('. $wpdb->prefix .'posts.post_status = "publish") ORDER BY '. $wpdb->prefix .'posts.post_date DESC';
The code you found modifies the main query on
pre_get_posts
and unsets the search terms soWP_Query
doesn’t run a real search query. What you want iswhich runs previously in
WP_Query::get_posts()
. This later on adds to theWHERE
clause:where the author part is:
and the MIME-Type part uses
wp_post_mime_type_where()
and isIf the
s
query var is set, you can useorderby
as well and modify it from a filter:The result of this will be the first
ORDER BY
SQL argument followed by whatever was specified as defaultORDER BY
argument. If there was no default, it will fall back to the search argument.Of course you still can then use the
posts_clauses
and the related filters. You do an early hands on inspection of your results to check if you managed to get a working query by hooking in onThis will give you the complete final SQL string.
Keep in mind that other plugins might as well jump in on the
pre_get_posts
,posts_request
orposts_clauses
,posts_*
filters and that you won’t really be able to work around such conflicts in a generic way.I’ve got multiple meta fields I need to search, pctracker_phone1 and pctracker_phone2 are just a couple. I’ve edited the SQL code so it searches for both. However the SQL code is going to be massive once all of the other metafields are added. Do you know how I could simplifier this: