Skip to content

Instantly share code, notes, and snippets.

@searchwpgists
Created October 30, 2025 07:09
Show Gist options
  • Select an option

  • Save searchwpgists/9c1fa6434dc92ccdfc61f6c915b08db7 to your computer and use it in GitHub Desktop.

Select an option

Save searchwpgists/9c1fa6434dc92ccdfc61f6c915b08db7 to your computer and use it in GitHub Desktop.
Prepend post type label to title for native main search
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment