0

I want to hide my custom post type from search results. So, I followed the codex and used:

exclude_from_search => 'true' while registering my custom post type.

That hides the custom post type and it does not appear in search results any more.

But now I am unable to load posts using get_posts but they appear fine with WP_Query. I wonder why it is happening.

1
  • Could you please elaborate how exactly get_posts() fails for you?
    – Rarst
    Commented Nov 4, 2016 at 16:56

2 Answers 2

1

get_posts() uses WP_Query, so you are probably providing wrong arguments, it's worth mentioning that the function has some default arguments.

This should get all posts of the CPT myCPT:

$args = array(
    'posts_per_page' => -1,
    'post_type'      => 'myCPT',
    'post_status'    => 'any',
);
$posts_array = get_posts( $args ); ?>
1

Can you try this code?

function excludePages($query) {
   if ($query->is_search) {
      $query->set('post_type', 'post');
   }
   return $query;
}
add_filter('pre_get_posts','excludePages');

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.