[ad_1]
What is it that you are trying to achieve with this code? Are you trying to exclude pages from website search results?
If so, you should use something like this:
add_filter(
'register_post_type_args',
static function ( $args, $name ) {
if ( 'page' === $name ) {
$args['exclude_from_search'] = true;
}
return $args;
},
10,
2
);
I only want posts to show in my frontend site search, not pages or custom post types.
Unfortunately the ‘exclude_from_search’ attribute causes issues with the custom post type taxonomy archive pages, which is why I prefer to use the custom query instead.
I see. Well, in that case if you wanna keep your existing snippet I recommend simply not altering any REST API requests by checking for the REST_REQUEST constant.
For example like this:
function purrdesign_excludePostType( $query ) {
if (
! $query->is_admin &&
$query->is_search &&
( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST )
) {
$query->set( 'post_type', 'post' );
}
return $query;
}
add_filter( 'pre_get_posts', 'purrdesign_excludePostType' );
Yes, that worked perfectly! Exactly what I was hoping for. Thanks so much for the quick response!
