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';
esc_attr()
orfilter_var()
on$_REQUEST['s']
to avoid getting hacked. Also post it as an answer instead of an update :)