The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Add a “New Post” Label to Recently Published WordPress Posts

Shared by Amit Dholiya · November 2, 2026

1 upvote
Back to Snippets

Steps

  1. Add the PHP code to your theme’s functions.php file or a PHP snippets plugin.
  2. Save and activate the snippet.
/**
 * Add a "New" label to recently published posts
 */
function custom_add_new_post_label( $title, $post_id ) {

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

    $published_time = get_post_time( 'U', true, $post_id );
    $days_ago       = ( time() - $published_time ) / DAY_IN_SECONDS;

    if ( $days_ago <= 3 ) {
        $title .= ' <span class="new-post-label">New</span>';
    }

    return $title;
}

add_filter( 'the_title', 'custom_add_new_post_label', 10, 2 );

Tested on

WordPress 6.8

Reported by the author on November 2, 2026. WPFolks has not verified this.

Not tested against WordPress 7.1.4. It may still work.

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

Proudly supported by