How to display Post Type label in Search Results
When using the default WordPress search, the results are displayed through your theme’s built-in search template file (usually named search.php). Most WordPress themes do not include post type information in their search result displays by default, showing only the post title, excerpt, and other standard elements. If you wish to show the post type label (for example, “Post,” “Page,” “Product,” etc.) alongside each search result, you will need to make a small customization either by editing your theme’s search template or by using WordPress hooks.
The first approach is to manually edit your theme’s search template file. Inside the main search loop, you can use the get_post_type_object() function to retrieve and display the post type label for each result. This gives you complete control over where and how the post type information appears within the search result layout.Alternatively, you can use a WordPress filter hook to automatically prepend the post type label before the title of each search result. This approach is easier and does not require modifying your theme files. You can add the following snippet by preparing a SearchWP Customizations Plugin or using a code snippet manager like WPCode:
| <?php | |
| // Prepend post type label to title for native main search | |
| add_filter( 'the_title', function( $title, $post_id ) { | |
| if ( in_the_loop() && ( is_search() || isset( $_REQUEST['swps'] ) ) ) { | |
| $post_type_obj = get_post_type_object( get_post_type( $post_id ) ); | |
| if ( $post_type_obj && ! empty( $post_type_obj->labels->singular_name ) ) { | |
| $label = esc_html( $post_type_obj->labels->singular_name ); | |
| $title = sprintf( '<span class="search-post-type">%s:</span> %s', $label, $title ); | |
| } | |
| } | |
| return $title; | |
| }, 10, 2 ); |
This code ensures that whenever a user performs a search, the post type name (such as “Page” or “Product”) is displayed before the title of each result in the search results list. It uses the WordPress the_title() filter and checks whether the current loop is part of a search query before applying the modification.
If you are using SearchWP’s custom Template results or Live Search, the above hook will not apply automatically because those results are generated differently. Instead, you can use SearchWP’s own filter hooks to modify the output of each result entry. The following example shows how to prepend the post type label before the title for both the SearchWP template and the Live Search results:
Here’s an example using a fallback image:
| <?php | |
| // Display post type label before title for SearchWP template and live search results | |
| function prepend_post_type_to_title( $data, $result ) { | |
| if ( $result instanceof \WP_Post ) { | |
| $post_type_obj = get_post_type_object( get_post_type( $result ) ); | |
| if ( $post_type_obj && ! empty( $post_type_obj->labels->singular_name ) ) { | |
| $label = esc_html( $post_type_obj->labels->singular_name ); | |
| $data['title'] = $label . ' - ' . $data['title']; | |
| } | |
| } | |
| return $data; | |
| } | |
| // Hook for SearchWP template | |
| add_filter( 'searchwp\results\entry\data', function( $data, $result ) { | |
| return prepend_post_type_to_title( $data, $result ); | |
| }, 20, 2 ); | |
| // Hook for SearchWP Live Search | |
| add_filter( 'searchwp_live_search_results_entry_data', function( $data, $result ) { | |
| return prepend_post_type_to_title( $data, $result ); | |
| }, 20, 2 ); |
With this customization in place, SearchWP will automatically display the post type name before the title in both its custom search template results and live search dropdown. This method provides a flexible and code-friendly way to give users additional context about the type of content they’re viewing in search results without directly modifying your theme files.

