• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Tips and Tricks HQ

  • Home
  • Blog
  • Projects
    • All Projects
    • Simple WP Shopping Cart
    • WP Express Checkout Plugin
    • WP Download Monitor
    • WP Security and Firewall Plugin
    • WP eStore Plugin
    • WP Affiliate Platform
    • WP eMember
    • WP Lightbox Ultimate
    • WP Photo Seller
  • Products
    • All Products
    • Checkout
  • Support
    • Support Portal
    • Customer Only Forum
    • WP eStore Documentation
    • WP Affiliate Software Documentation
    • WP eMember Documentation
  • Contact

Add a Status Text Next to Your WordPress Post Title

You are here: Home / Web Development / Add a Status Text Next to Your WordPress Post Title

Last updated: June 24, 2017





In this tutorial I will show you how you can add a status text next to the post title based on a condition.

There are two different ways you can handle this:

  1. Show the status text dynamically when WordPress is rendering the post title.
  2. Update the post title text in the wp_posts database table using a cronjob.

For this tutorial we will show an “Expired” message before the post title if the post’s published date is older than 14 days. The same concept can be applied to do any kind of conditional check and update the post title accordingly.

Method #1) Dynamically Update the Post Title

We can use the “the_title” filter to modify the post title when it is being rendered.



The following code example shows how to do it (read the comment in the code for explanation):

add_filter('the_title', 'tthq_add_expired_status_to_title');
function tthq_add_expired_status_to_title($title) {
    //Check if this is a page or not
    if (is_page()){
        //We don't want to do it for pages.
        return $title;
    }
    //Check if this is an attachment
    if (is_attachment()){
        //is_single() can be true for attachment so it is good to check is_attachment()
        return $title;
    }    
    //Check if we are in the loop
    if (!in_the_loop()){
        return $title;
    }
    
    //Initialize the date objects
    $created = new DateTime(get_the_date('Y-m-d g:i:s'));
    $current = new DateTime(date('Y-m-d g:i:s'));

    //The date_diff objects
    $created_to_today = date_diff($created, $current);

    //Check if the post published date is older than 14 days.
    $has_expired = ($created_to_today && $created_to_today->days > 14 ) ? true : false;

    //Adds status text before the post title
    if($has_expired){
        //Modify the post title to add the expired message in there.
        $markup = 'Expired';
        $title = $markup . ' ' . $title;
    }

    //Return post title
    return $title;
}

You can also take it one step further by collecting a custom value in the post editor then using that value in the above code. This tutorial shows you how you can add a meta box in the post editor so you can save custom value for each of your posts.

Method #2) Update Post Title in the Database

You can create a small plugin that creates a “daily” or “weekly” cronjob for your site. When the cronjob is triggered, you can do your check then update the post title using the wp_update_post() function.

Below is an example of how you can schedule a daily cronjob when your custom plugin is activated:

function tthq_custom_plugin_activate() {
    wp_schedule_event(time(), 'daily', 'tthq_custom_daily_cron_event');
}
register_activation_hook(__FILE__, 'tthq_custom_plugin_activate');

You will then write code in the tthq_custom_daily_cron_event() function which will do the checks in a loop and update post title accordingly.

The following code block shows an example of how to update the post title in the wp_posts database table:

//Lets say we want to update the post title of post ID 20
$my_post = array(
    'ID' => 20,
    'post_title' => 'This is the UPDATED post title.',
);

//Update the post title in the database
wp_update_post( $my_post );

Check the WordPress codex page to learn more about the wp_update_post() function.

Related Posts

  • Query or show a specific post in wordpress
  • How to Use Firebug to Modify Your WordPress Site’s CSS (Video Tutorial)
  • How To Create an Attractive Subscribe & Follow Box For Your WordPress Blog
  • Upgrading the Core WordPress Custom Menu Widget

Web Development,  Wordpress Blog Setup,  Code example,  WordPress tutorial,  WordPress Tweaks

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Featured & Popular Articles

Video Answers to Top WordPress QuestionsWordPress Optimization Tips and Tricks for Better Performance and SpeedEssential WordPress Security Tips - Is Your Blog Protected?WordPress Simple PayPal Shopping Cart PluginTop 15 Search Engine Optimization (SEO) Techniques I Forget to DoList of the Best and Must Use WordPress PluginsHow do I Start a Blog and Make Money Online?Good Domain Name Picking Tips for Your Blog SetupFind Out Which WordPress Web Hosting Company Offers the Cheapest and Reliable Web Hosting Solution

Featured WordPress Plugins

wordpress estore plugin
wordpress membership plugin
WP Express Checkout Plugin
WordPress Lightbox Ultimate Plugin
WordPress photo seller plugin
wordpress affiliate plugin

Recent Posts

  • Accept Donations via PayPal from Your WordPress Site Easil [...]
  • Buy Now Button Graphics for eCommerce Websites [...]
  • Subscription Button Graphics for eCommerce Websites [...]
  • Adding PayPal Payment Buttons to Your WordPress Sidebar Ea [...]
  • PayPal QR Codes [...]

Comment & Socialize

  • @Rodrigo Souza, Thank you f ...
    - admin
  • The example for 'slm_add_ed ...
    - Rodrigo Souza
  • @Ron, All the valid transac ...
    - admin
  • Hello, when people have sel ...
    - Ron
  • We have hte following featu ...
    - admin

Check out our solutions

View our WordPress plugin collection and start using them on your site.

Our WordPress Solutions

Footer

Company

  • About
  • Privacy Policy
  • Terms and Conditions
  • Affiliate Login

Top WordPress Plugins

  • Simple Shopping Cart
  • PayPal Donations
  • WP Express Checkout
  • WP eStore
  • WP eMember

Blogging Tips

  • How to Start a Blog
  • Selecting a Good Domain
  • Cheap WP Hosting
  • WP Video Tutorials
  • Simple SEO Tips

Search


Keep In Touch

Copyright © 2023 | Tips and Tricks HQ