-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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