最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Pre_get_posts filter overwrites all search functionality

programmeradmin1浏览0评论

So I have the out of the box wordpress 'Search' widget that I want to expand. I used the below filter to have the ability to search for my custom post types inside the widget:

add_filter( 'pre_get_posts', function($query) {
    if ($query->is_search) {
        $query->set('post_type', [
            'post', 'profile' ,'recipe', 'dish'
        ]);
    }
});

The problem that I discovered is that it overwrote ALL search items on the website, including the search box inside the admin panel 'Posts', where I was getting custom post type results although they didn't live there.

Does anyone know how to improve the search functionality on the search widget to include custom post types without overwriting ALL search boxes throughout the site?

So I have the out of the box wordpress 'Search' widget that I want to expand. I used the below filter to have the ability to search for my custom post types inside the widget:

add_filter( 'pre_get_posts', function($query) {
    if ($query->is_search) {
        $query->set('post_type', [
            'post', 'profile' ,'recipe', 'dish'
        ]);
    }
});

The problem that I discovered is that it overwrote ALL search items on the website, including the search box inside the admin panel 'Posts', where I was getting custom post type results although they didn't live there.

Does anyone know how to improve the search functionality on the search widget to include custom post types without overwriting ALL search boxes throughout the site?

Share Improve this question asked Feb 26, 2020 at 16:06 DevSemDevSem 2092 silver badges11 bronze badges 3
  • You would need to overwrite the search widget so that it submits an additional $_GET param indicating that it's "a widget" so you can check against it in pre_get_posts. If you're just looking to stop it from running on admin you can check against is_admin() and return early. – Howdy_McGee Commented Feb 26, 2020 at 16:14
  • @Howdy_McGee, are there examples out there with the $_GET being used? – DevSem Commented Feb 26, 2020 at 16:16
  • @Howdy_McGee, is it possible to adjust the search functionality on custom post types? Under 'Posts', only 'post' post_type shows, but under my 'recipe' post_type, both 'post' and 'recipe' show. – DevSem Commented Feb 26, 2020 at 16:25
Add a comment  | 

1 Answer 1

Reset to default 2

That's easy, pre_get_posts applies to all queries, not just those on the frontend. So if you don't want it to run on admin queries, test if you're in the admin and exit early!

You might also want to verify that you're in the main query

add_filter( 'pre_get_posts', function( \WP_Query $query) {
    if ( is_admin() ) {
        return;
    }
    if ( ! $query->is_main_query() ) {
        return;
    }

Remember, pre_get_posts runs on all queries, regardless of where, be it admin, frontend, XMLRPC, REST API, etc. It will only run on the frontend if you tell it to only run on the frontend.

发布评论

评论列表(0)

  1. 暂无评论