How can I add custom post types to the search in WordPress? (Instruction, 2023)

Marc Wag­ner

Janu­ary 24, 2023

4 min read|

By default, cus­tom post types are not included in Word­Press search. But as always with Word­Press, the­re is a hook that we use right now to expand/customize the search. In no time you have cus­to­mi­zed the search.

How to add Custom Post Types to WordPress Search with PHP #

The hook we need to extend the search is pre_get_posts. Below I’ll show you seve­ral snip­pets that will cus­to­mi­ze your search.

Code snippet for PHP 8

If your ser­ver alre­a­dy sup­ports PHP 8, we recom­mend the fol­lo­wing snip­pet to cus­to­mi­ze your search:

/**
 * This function extends the default WordPress WP_Query object 
 * to include an array of post types instead of the default 'post'
 * post type.
 */
function add_post_types_to_search_query(WP_Query $query): void{
  // Skip if this call has been triggered for an administrative interface page.
  if(is_admin()){
    return;
  }
  
  // Skip this call if this is not the search query and if this is not the main query.
  if(!$query->is_search() || !$query->is_main_query()){
    return;
  }
  
  // Define which post types should be searched
  $list_of_post_types = ['post', 'pages'];
  
  // Now - if we get to this point, this must be the search query which we will modify.
  $query->set('post_type', $list_of_post_types);
}
add_action('pre_get_posts', 'add_post_types_to_search_query', 10, 1);

Alternative: Code Snippet for PHP 7

You should only use this code if PHP 8 is not yet run­ning on your ser­ver or your site is not com­pa­ti­ble with PHP 8.

/**
 * This function extends the default WordPress WP_Query object 
 * to include an array of post types instead of the default 'post'
 * post type.
 */
function add_post_types_to_search_query($query){
  // Skip if this call has been triggered for an administrative interface page.
  if(is_admin()){
    return;
  }
  
  // Skip this call if this is not the search query and if this is not the main query.
  if(!$query->is_search() || !$query->is_main_query()){
    return;
  }
  
  // Define which post types should be searched. Replace/Extend by your post types.
  $list_of_post_types = array('post', 'pages');
  
  // Now - if we get to this point, this must be the search query which we will modify.
  $query->set('post_type', $list_of_post_types);
  
}
add_action('pre_get_posts', 'add_post_types_to_search_query', 10, 1);

Long-term easy to maintain thanks to OOP, code snippet

Here’s an exam­p­le of how you can map the who­le thing with a class and name­spaces if you want to keep main­ten­an­ce low in the long run.

namespace My_Search{
  /**
   * This class extends the default WordPress WP_Query object
   * to include an array of post types instead of the default 'post'
   * post type.
   */
  class Search_Query_Post_Type_Modifier{
    /**
     * There should never be more than one object of this class therfor
     * we need to create a Singleton
     */
    private static ?Search_Query_Post_Type_Modifier $_instance = null;
    
    /**
     * This function will allow us to access the instance of this class
     */
    public static function get_instance(): Search_Query_Post_Type_Modifier{
        if(null === self::$_instance){
           self::$_instance = new Search_Query_Post_Type_Modifier();
        }
      
      	return self::$_instance;
    }
    
    /**
     * Private constructor to disable multiple instances of this object.
     */
    private function __construct(){
     	add_action('pre_get_posts', [$this, 'add_post_types_to_query']);
    }
    
    /**
     * Add the post types to the search query
     */
    public function add_post_types_to_query(WP_Query $query): void{
  		// Skip if this call has been triggered for an administrative interface page.
  		if(is_admin()){
           	return;
        }

        // Skip this call if this is not the search query and if this is not the main query.
        if(!$query->is_search() || !$query->is_main_query()){
          	return;
        }

        // Define which post types should be searched
        $list_of_post_types = ['post', 'pages'];

        // Now - if we get to this point, this must be the search query which we will modify.
        $query->set('post_type', $list_of_post_types);
  	}
  }
  
  /**
   * Instantiate the object
   */
  Search_Query_Post_Type_Modifier::get_instance();
}

As few lines of code as possible: #

This ver­si­on is meant to sim­ply use as few lines of code as pos­si­ble :)

add_action('pre_get_posts', function(WP_Query $query){
   if(!is_admin() && $query->is_search() && $query->is_main_query()){
     $query->set('post_type', ['post','page']);
   }
}, 10, 1);
88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar