Auto post from draft after x time

      No Comments on Auto post from draft after x time

“Auto post after X time” refers to a feature or functionality that automatically publishes a post on a WordPress website after a specified amount of time has elapsed. This feature is useful for scheduling content to be published at a future date and time without manual intervention.

With “auto post after X time” functionality, users can create a post, set a delay time for publishing (X time), and the post will automatically go live after that period. This can be particularly helpful for maintaining a consistent publishing schedule, managing content in advance, or timing posts to coincide with specific events or promotions.

This functionality can be achieved using plugins or custom code in WordPress

Method – 1 (with plugin)

DX Auto Publish” is a WordPress plugin that provides automatic publishing functionality for posts and pages based on a specified delay time. This plugin allows users to schedule content to be published after a set amount of time has elapsed since the post was created or last updated.The plugin not available in WordPress plugin store.Also the plugin language in chinese.

   For download the code

  • Find the “Code” Button: On the repository’s main page, you’ll see a green button labeled “Code”. Click on it.
  • Select “Download ZIP”: After clicking the “Code” button, a dropdown menu will appear. Click on the “Download ZIP” option. This will initiate the download of the repository’s code as a ZIP file to your computer.
  • Save the ZIP File: Your browser will typically prompt you to choose where to save the ZIP file on your computer. Select the desired location and click “Save” or “OK”.

How to install a plugin

Now set time in sec Order by Random or Id and update .

Time to sec converter Here (Google link)

Enjoy………

Method-2 (without plugin)

To automatically publish posts from draft status after a certain amount of time in WordPress, you can achieve this functionality through custom code. Below is a step-by-step guide on how to implement this feature:

 

function auto_publish_draft_posts() {
    // Get all draft posts
    $draft_posts = get_posts( array(
        'post_status' => 'draft',
        'numberposts' => -1,
    ) );

    // Iterate through each draft post
    foreach ( $draft_posts as $post ) {
        // Get the current time and post creation time
        $current_time = current_time( 'timestamp' );
        $post_time = strtotime( $post->post_date );

        // Define the delay time in seconds (e.g., 1 hour = 3600 seconds)
        $delay_time = 3600; // Adjust this value as needed

        // Check if the post creation time + delay time is less than the current time
        if ( $post_time + $delay_time <= $current_time ) { // Change the post status to publish wp_update_post( array( 'ID' => $post->ID,
                'post_status' => 'publish',
            ) );
        }
    }
}
// Schedule the function to run hourly
add_action( 'init', 'auto_publish_draft_posts' );

Open your theme’s functions.php file, which is typically located in the wp-content/themes/your-theme-name/ directory.

Modify the $delay_time variable to set the desired delay before posts are automatically published. The delay time is specified in seconds. For example, if you want posts to be published 1 hour after creation, set $delay_time = 3600;. Save Your Changes

Create a new post and save it as a draft. Wait for the specified delay time, and the post should automatically be published.This code snippet will automatically publish draft posts after the specified delay time has elapsed. Make sure to test it thoroughly on a staging or development site before implementing it on a live site. Additionally, consider using a child theme to add custom code to ensure your changes are not overwritten during theme updates.

Leave a Reply

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