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?
2 Answers
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:
When
wp_insert_post()
is done it callswp_transition_post_status()
.Which executes several hooks, including
"{$new_status}_{$post->post_type}"
which unwraps to something likefuture_post
.- When post type is registered by
register_post_type()
it adds_future_post_hook()
function to'future_' . $post_type
hook and so it executes. - That function creates single
WP-Cron
schedule at the date of post, post's ID in data and name ofpublish_future_post
. - 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 topublished
usingwp_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.
-
@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.– RarstDec 26, 2011 at 18:12
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.