0

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 
3
  • I'm not sure I understand the problem: is page post type also popping up in your search results? Maybe you could add the generated search SQL query to your question?
    – birgire
    Commented Feb 2, 2015 at 11:09
  • Yes page post_type is also coming up in the results that is what is driving me nuts. (Thanks for editing the code)
    – Colin
    Commented Feb 2, 2015 at 19:09
  • Does it happens on a vanilla install (default theme & no plugins) ? PS: You can also check out the generated SQL query with echo $query->request;
    – birgire
    Commented Feb 3, 2015 at 0:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.