Skip to content

Instantly share code, notes, and snippets.

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

  • Save searchwpgists/974e64eaf917f28ffd33cd8d0f190e2d to your computer and use it in GitHub Desktop.

Select an option

Save searchwpgists/974e64eaf917f28ffd33cd8d0f190e2d to your computer and use it in GitHub Desktop.
Display post type label before title for SearchWP template and live search results
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment