How To Include Custom Post Types In The WordPress Search Results

Mon, Nov 14th, 2022

Including custom post types in the WordPress search results can be a bit tricky, but it’s definitely worth the effort. In this snippet, we’ll show you how to include your custom post types in the results, as well as how to customize the search experience for your users.

If you’re a web designer or developer, then you know how important it is to make your client’s website searchable. In this snippet post, we’ll show you how to include custom post types in the WordPress search results. This will allow your clients’ visitors to find the content they’re looking for quickly and easily. So, whether you’re just getting started with WordPress or are a seasoned pro, this code snippet is for you! Let’s get started!

This function is designed to help you filter your search results by adding new arguments. As seen below, it will return content from each of these custom post types: posts, players, clubs, & folio – all simply changing or deleting the names in array below!


<?
function amdd_include_custom_post_types_in_search_results ( $query ) {
    if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
        $query->set( 'post_type', array( 'post', 'clubs', 'match', 'players', 'folio' ) );
    }
}
add_action( 'pre_get_posts', 'amdd_include_custom_post_types_in_search_results' );
© 2022 Craig Allen. All rights reserved. Powered by Allen Media