How to change category of a post automatically in WordPress?

Change category of wordpress post when date is today

  • So I am using the plugin 'the future is now' on a wordpress site, the site is for event news, once an event has happened i need it to move into from the 'events' category to the 'results'. The plugin 'the future is now' takes care of displaying future posts but I cannot find anything online as to how to change the post category when the date is 1 day past. For example an event on the 11/04/2014 should change categories at 00:01 12/04/2014.

  • Answer:

    You will need a couple things in order to change the category using PHP <?php $date = the_date(); $today = date("F j, Y"); if($date < $today){ $post_ID = get_the_ID(); $post_categories = array(CATEGORY_NUMBER_FOR_RESULTS); wp_set_post_categories( $post_ID, $post_categories ); } ?> You will need to place this in the while-loop you use to get the "Events" category posts. While this method should work I highly recommend NOT using it because if at any point the post_ID or post_categories messes up it can create orphan posts which will need SQL database manipulation to fix.

Chris Naylor at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.