I have a CPT that has multiple fields. I have a custom input form that takes a name as the input and this is stored in the $surname variable. I need to search my CPT for that name either as the CPT title or stored in one of the fields. I then need to display these one or more CPT's on a single custom template page.
This is my current code
$args1 = array(
's' => $surname,
'post_type' => 'studies'
);
$query = new WP_Query ($args1);
This works fine and finds the name as I want BUT it also finds and returns pages that also have the same name on them. I have tried turning on exact in the query parameters and that stops displaying the pages but also stops searching the custom fields in the CPT.
To get the data from the CPT I am just using get_post_meta like this for example:
get_post_meta($post->ID, 'study_location', true)
I have spent several hours searching on here and on Google without success, I would have thought with specifying the post type as studies it would not search post type of page too.
This is the query that is being output so it seems to be ignoring the content of args1
SELECT SQL_CALC_FOUND_ROWS ts14_posts.ID FROM ts14_posts WHERE 1=1 AND (((ts14_posts.post_title LIKE '%Pennyfather%') OR (ts14_posts.post_content LIKE '%Pennyfather%'))) AND ts14_posts.post_type IN ('post', 'page', 'attachment', 'studies', '1.23') AND ((ts14_posts.post_status = 'publish')) ORDER BY ts14_posts.post_title LIKE '%Pennyfather%' DESC, ts14_posts.post_date ASC LIMIT 0, 10
echo $query->request;