Skip to main content
Login Join
Snippet · PHP

Display a “New” Badge on Recently Published Posts

Shared by Amit Dholiya · August 3, 2026 · @the_title

12 views
1 upvote
Back to Snippets

Highlight newly published content by automatically displaying a “New” badge next to post titles for a specified number of days. This helps attract attention to fresh content and improves user engagement.

Steps

  1. Open your active theme’s functions.php file or a custom functionality plugin.
  2. Add the code below.
  3. Save the file.
  4. Newly published posts (within the last 7 days) will automatically display a New badge in their titles.

 

Benefits

/**
 * Add a "New" badge to recently published posts.
 */
function wpfolks_new_post_badge( $title, $post_id ) {

    if ( is_admin() || 'post' !== get_post_type( $post_id ) ) {
        return $title;
    }

    $days = 7;

    $published = get_post_time( 'U', true, $post_id );

    if ( ( time() - $published ) < ( DAY_IN_SECONDS * $days ) ) {
        $title .= ' <span class="wpfolks-new-badge" style="background:#d63638;color:#fff;padding:2px 6px;border-radius:3px;font-size:11px;margin-left:6px;">New</span>';
    }

    return $title;
}
add_filter( 'the_title', 'wpfolks_new_post_badge', 10, 2 );
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments