Wie kann ich in WordPress die Suche um eigene Post Types erweitern? (Anleitung, 2023)

Standardmäßig werden eigene Post Types (Custom Post Types) nicht in der WordPress Suche berücksichtigt. Aber wie immer bei WordPress gibt es auch hier einen Hook, mit dem wir jetzt gleich die Suche erweitern/anpassen. Im Handumdrehen hast du die Suche angepasst.

So erweiterst du die WordPress Suche mit PHP um Custom Post Types #

Den Hook den wir für die Erweiterung der Suche benötigen, lautet pre_get_posts. Im Folgenden zeige ich dir verschiedene Snippets, die deine Suche anpassen werden.

Code Snippet für PHP 8 #

Falls auf deinem Server bereits PHP 8 unterstützt wird, empfehlen wir dir folgendes Snippet, um deine Suche anzupassen:

/**
 * 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 für PHP 7 #

Diesen Code solltest du nur verwenden, wenn PHP 8 noch nicht auf deinem Server läuft bzw. deine Seite nicht mit PHP 8 kompatibel ist.

/**
 * 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);

Langfristiger einfach zu Pflegen dank OOP, Code Snippet #

Hier ein Beispiel, wie du das Ganze mit einer Klasse und Namespaces abbilden kannst, falls du langfristig den Wartungsaufwand gering halten möchtest.

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();
}

Möglichst wenig Zeilen Code: #

Diese Version ist dafür gedacht, einfach möglichst wenig Zeilen Code zu verwenden :)

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);
Hier klicken, um den Beitrag zu bewerten
[Gesamt: 0 Durchschnitt: 0]

Hinterlasse einen Kommentar

Titel