Tips and Tricks Latest Post

Tips and Tricks Latest Post


Add a Status Text Next to Your WordPress Post Title

Posted: 24 Jun 2017 06:07 AM PDT

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.

No comments: