• 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 4 years, 9 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 15 replies - 1 through 15 (of 44 total)
  • 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

    SPECIAL :

    View post on imgur.com

    Thread Starter Mikel

    (@ogmic)

    Hi @corrinarusso please can put me through… How do I setup Cron and where do I paste this code? – Code snippet?

    Thread Starter Mikel

    (@ogmic)

    @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.

    Hi @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.www.ads-software.com/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/#:~:

    That’s it.
    You may have to disable the WordPress cron, like this :
    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

    (@ogmic)

    @corrinarusso thanks so much… Will do that now

    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

    (@ogmic)

    @corrinarusso show me how please… I am new to WordPress.

    Thread Starter Mikel

    (@ogmic)

    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?

    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

    (@ogmic)

    Okay.. thanks

    Thread Starter Mikel

    (@ogmic)

    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?

    According to this article, it’s fine :

    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

    (@ogmic)

    Okay, Thanks so so much… I am deeply grateful

    Thread Starter Mikel

    (@ogmic)

    @corrinarusso sorry to disturb you, please what do I enter in the command field in Cron job?

    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)
  • The topic ‘Need help with a code’ is closed to new replies.