0

I'm a database noob so I'm a little bit clueless as to how to make a scheduled post via a plugin. Basically, does WP provide a reference API that would allow a plugin to create a scheduled post, like a user can do through the dashboard?

1
  • There are a number of plugins that you can use to schedule posts. Dec 25, 2011 at 7:09

2 Answers 2

3

There isn't really an API for it, because from usage perspective it is quite simple.

If data passed to wp_insert_post() has publish or future value in post_status field and date in the future then it is inserted in database with that date and future in post_status. Effectively it is already considered scheduled.

However something would need to happen to make that post published on that date, so this sets in motion several more pieces of code:

  1. When wp_insert_post() is done it calls wp_transition_post_status().

  2. Which executes several hooks, including "{$new_status}_{$post->post_type}" which unwraps to something like future_post.

  3. When post type is registered by register_post_type() it adds _future_post_hook() function to 'future_' . $post_type hook and so it executes.
  4. That function creates single WP-Cron schedule at the date of post, post's ID in data and name of publish_future_post.
  5. That schedule name has check_and_publish_future_post() function hooked to it, that on run checks if post by that ID is scheduled and up to be live and if so flips its status to published using wp_publish_post().

So the simplest way to create scheduled post is to just follow native mechanics and create it with wp_insert_post(), while passing future date.

1
  • @Ankur you are welcome :) For some reason I thought logic is much simpler, so I got sucked into challenge of tracing through all of the details.
    – Rarst
    Dec 26, 2011 at 18:12
1

If you are using the xmlrpc way of posting articles, then you have the option of not posting your articles immediately.

    //$client = new IXR_Client('http://localhost/giftanexperience/xmlrpc.php');
    $client = new IXR_Client('<domain>/xmlrpc.php');

    //if (!$client->query('wp.getCategories','', <username>,<password>)) {
    //      die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
    //}
    echo $client->getResponse();

    $content['title'] = $title;
    $content['description'] = $description;
    $content['categories'] = $categories;
    $content['mt_keywords'] = $tags;

    if (!$client->query('metaWeblog.newPost','', <username>,<password>, $content, true)) {
            die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
    }
    echo $client->getResponse() . "\n" ;    //with Wordpress, will report the ID of the new post

Here you can substitute true with false to not publish your article.

For a detailed sample, you can look up: http://www.home-working.info/index.php/creating-a-wordpress-website-via-xml-rpc-using-affiliate-data-feeds/

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.