# Content Type Labels in Search Results

> Shows a small label such as "Page", "Post" or "Product" in front of each search result title, so visitors can tell results apart at a glance.

- Type: PHP
- Runs: Run everywhere
- Category: [Search](https://wpide.com/library/category/search/)
- Tags: search, labels, results
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/search-result-type-labels/

## Code

```php
add_filter( 'post_class', function ( $classes, $extra, $post_id ) {
    if ( is_search() && ! is_admin() ) {
        $classes[] = 'wpide-result-type-' . sanitize_html_class( get_post_type( $post_id ) );
    }

    return $classes;
}, 10, 3 );

add_action( 'wp_head', function () {
    if ( ! is_search() ) {
        return;
    }

    $css = '';

    foreach ( get_post_types( array( 'exclude_from_search' => false ), 'objects' ) as $type ) {
        $label = str_replace( array( '\\', '"', '<', '>' ), '', $type->labels->singular_name );

        $css .= '.wpide-result-type-' . sanitize_html_class( $type->name ) . ' :is(.entry-title,.wp-block-post-title)::before{content:"' . $label . '";}';
    }

    // A small pill in front of each result title, such as "Page" or "Product".
    $css .= '[class*="wpide-result-type-"] :is(.entry-title,.wp-block-post-title)::before{display:inline-block;margin-right:.5em;padding:.15em .6em;border-radius:999px;border:1px solid currentColor;font-size:.55em;font-weight:600;letter-spacing:.03em;line-height:1.4;text-transform:uppercase;vertical-align:middle}';

    echo '<style id="wpide-result-type-labels">' . $css . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput -- labels stripped of quotes and markup above.
} );
```

## Notes

Uses the singular name of each post type; works with themes that use the standard entry-title or Post Title block classes.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Content Type Labels in Search Results" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
