Title: Need help with a code
Last modified: June 11, 2020

---

# Need help with a code

 *  [Mikel](https://wordpress.org/support/users/ogmic/)
 * (@ogmic)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/)
 * Can anyone help with a custom hook/code to auto change the post in a category
   to another category every day at exactly 12:00AM.
 * I have three categories ( general, special, free )… I have contents with this
   categories; general & free. So I want any post in the FREE category to change
   automatically to SPECIAL 12:00AM Daily. So I will have the post in General + 
   SPECIAL category.
    -  This topic was modified 6 years ago by [Jan Dembowski](https://wordpress.org/support/users/jdembowski/).
      Reason: Moved to Fixing WordPress, this is not an Everything else WordPress
      topic

Viewing 15 replies - 1 through 15 (of 44 total)

1 [2](https://wordpress.org/support/topic/need-help-with-a-code/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/need-help-with-a-code/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/need-help-with-a-code/page/2/?output_format=md)

 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12978890)
 * There are several methods, and some will have a lot more validation than others.
   
   I would probably use WP CLI comments, but if you are not familiar with them, 
   and if you are sure about the business rules : – collect all posts with Category
   FREE and replace with SPECIAL, you could do something very basic like this :
 * 1. Create a file in your child theme directory called movecategory.php
 * 2. Setup and kick off your cron every day at midnight :
    0 0 * * * php /path-
   to-your-script/movecategory.php
 * – if you are using WP CLI get the IDs of posts with the FREE category :
    wp post
   list –category_name=’FREE’ –format=ids
 * otherwise, from the wpdb use this :
    $oldslughere = ‘FREE’ $newslughere= 86; //
   see here we are using the category_id $category = get_category_by_slug( $oldCatSlug);
   check to make sure there is a result : if (category) post = get_posts, category_name
   => ‘FREE’ if (posts) foreach post wp_set_post_categories (postid->id, $newslughere,
   false) // use true if you want append the category
 * That’s it.
    FREE :
 * > [View post on imgur.com](https://imgur.com/a/IgWk1JY)
 * SPECIAL :
 * > [View post on imgur.com](https://imgur.com/a/iWXDmbg)
 *  Thread Starter [Mikel](https://wordpress.org/support/users/ogmic/)
 * (@ogmic)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979276)
 * Hi [@corrinarusso](https://wordpress.org/support/users/corrinarusso/) please 
   can put me through… How do I setup Cron and where do I paste this code? – Code
   snippet?
 *  Thread Starter [Mikel](https://wordpress.org/support/users/ogmic/)
 * (@ogmic)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979280)
 * [@corrinarusso](https://wordpress.org/support/users/corrinarusso/) I really really
   appreciate your response, but I am just kinda lost somewhere.. I will appreciate
   if you can make it simpler please I beg of you.
 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979307)
 * Hi [@ogmic](https://wordpress.org/support/users/ogmic/)
 * I would prefer that you were a lot stricter in your requirements. There is no
   data validation in this at all, and I don’t really know the scope of your entire
   site or your request. So to be clear, the only requirement being met here is 
   to change all posts with Category FREE to category SPECIAL at midnight every 
   day.
 * The code above does not run, it’s only pseudo code.
 * 1. Create a file in your child theme directory called movecategory.php
    Do you
   already have a child theme set up for your site ? If you don’t have a child theme
   set up, then login to your control panel, or ftp and create a file called movecategory.
   php
 * Copy and paste in this code into your php file (I’ve tested it, it works, but
   it’s not optimized!) :
 *     ```
       <?php
       // change the path to your wp-load.php file 
       require_once('/home/full-path-to-your-root-wordpress/public_html/your-website.com/wp-load.php'); 
       //$output = shell_exec('wp post list --category_name="FREE" --format=ids 2>1&');
       //echo $output."\n";
       global $wpdb;
       $oldCatSlug = 'FREE';
       $newCatSlug = 86; // you need to insert an integer here that matches your SPECIAL category
       $category = get_category_by_slug( $oldCatSlug );
   
       if ( $category ) {
           $posts = get_posts( array(
               'numberposts' => -1,
               'category_name' => 'FREE',
               )
           );
           if ( $posts ) {
               foreach ( $posts as $post ) {
                   // update categories the match FREE
                   // see https://codex.wordpress.org/Function_Reference/wp_set_post_categories
                   wp_set_post_categories( $post->ID, $newCatSlug, false ); // set this to true if you want th
       is category appended
               }
           }
       }
       ?>
       ```
   
 * Save the file.
 * Login to your control panel, and set up a cron – this is how it is done in Siteground:
   
   [https://www.siteground.com/tutorials/cpanel/cron-jobs/#:~](https://www.siteground.com/tutorials/cpanel/cron-jobs/#:~):
 * That’s it.
    You may have to disable the WordPress cron, like this : [https://www.siteground.com/tutorials/wordpress/real-cron-job/](https://www.siteground.com/tutorials/wordpress/real-cron-job/)
 * It’s very simple.
    But if you are struggling with how to create files, and upload
   them, you may need to ask a developer who has access to your website to help 
   out.
 *  Thread Starter [Mikel](https://wordpress.org/support/users/ogmic/)
 * (@ogmic)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979308)
 * [@corrinarusso](https://wordpress.org/support/users/corrinarusso/) thanks so 
   much… Will do that now
 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979312)
 * Great!
 * You can change the time or frequency of the cron to make sure it works as expected.
   It would also be best if to make sure the file does not take any input parameters.
 * I can show you how if you need.
 *  Thread Starter [Mikel](https://wordpress.org/support/users/ogmic/)
 * (@ogmic)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979313)
 * [@corrinarusso](https://wordpress.org/support/users/corrinarusso/) show me how
   please… I am new to WordPress.
 *  Thread Starter [Mikel](https://wordpress.org/support/users/ogmic/)
 * (@ogmic)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979314)
 * Here
 *     ```
       change the path to your wp-load.php file 
       require_once('/home/full-path-to-your-root-wordpress/public_html/your-website.com/wp-load.php')
       ```
   
 * Do I need to enter my site address?
 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979319)
 * You need to login to your control panel for your hosting, and get the full path
   to your wp-load.php file.
 * If you can’t find it you’ll need to ask your host.
 *  Thread Starter [Mikel](https://wordpress.org/support/users/ogmic/)
 * (@ogmic)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979320)
 * Okay.. thanks
 *  Thread Starter [Mikel](https://wordpress.org/support/users/ogmic/)
 * (@ogmic)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979321)
 * One more thing please, I have LiteSpeed Cache plugin running on my site, and 
   to the best of my knowledge.. I think it uses WordPress Cron, is it safe if I
   disable the WordPress Cron and if I did not disable it, will it affect the code
   you just gave me?
 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979333)
 * According to this article, it’s fine :
 * [https://blog.litespeedtech.com/2017/05/17/wpw-using-lscache-for-wp-with-scheduled-posts/](https://blog.litespeedtech.com/2017/05/17/wpw-using-lscache-for-wp-with-scheduled-posts/)
 * But test it out.
    WP Cron is required for scheduled posts, but you can see from
   the article there are other methods. WP Cron also runs using user activity. While
   the cron you are setting up runs on the server, without any user intervention
   or input.
 *  Thread Starter [Mikel](https://wordpress.org/support/users/ogmic/)
 * (@ogmic)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979350)
 * Okay, Thanks so so much… I am deeply grateful
 *  Thread Starter [Mikel](https://wordpress.org/support/users/ogmic/)
 * (@ogmic)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979372)
 * [@corrinarusso](https://wordpress.org/support/users/corrinarusso/) sorry to disturb
   you, please what do I enter in the command field in Cron job?
 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/need-help-with-a-code/#post-12979378)
 * You need to enter in php, <space>, then the exact full path to your file named
   movecategory.php
 * So like this :
 * php /home/xxxxxx/public_html/movecategory.php

Viewing 15 replies - 1 through 15 (of 44 total)

1 [2](https://wordpress.org/support/topic/need-help-with-a-code/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/need-help-with-a-code/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/need-help-with-a-code/page/2/?output_format=md)

The topic ‘Need help with a code’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 44 replies
 * 2 participants
 * Last reply from: [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * Last activity: [5 years, 11 months ago](https://wordpress.org/support/topic/need-help-with-a-code/page/3/#post-13042864)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
