Sign up ×

I'm trying to write a php script that I can run out of cron to create new Wordpress pages.

Unfortunately, I can't find any documentation on how to do this. I'm using the WP scripts rather than directly manipulating the SQL...but still no joy. The below runs with no errors...but also produces no pages. I am sad.

Anyone know how to do this?

        include ('/some/path/wordpress/wp-admin/admin.php');
        include ('/some/path/wordpress/wp-includes/post.php');

        $to_insert = array();

        $to_insert['post_author'] = 0;
        $to_insert['post_content'] = '<h1>Here is my info</h1>\n<ul><li>one</li><li>two</li></ul>\n';
        $to_insert['post_type'] = 'page';
        $to_insert['post_status'] = 'publish';
        $to_insert['post_title'] = 'We Must Test';

        $result = wp_insert_post($to_insert,true);

        if ( is_wp_error($result) ) {
                echo $result->get_error_message();
share|improve this question

migrated from May 31 '11 at 20:56


5 Answers 5

You should be including this file:

    include( "/some/path/wordpress/wp-config.php" );

That'll get you to where you can use the wp_insert_post() function.

If this is a multi-site blog, make sure to use the switch_to_blog() function first.

share|improve this answer
No, please include wp-load.php instead of the config file. The wp-config file can live in different places, wp-load can't. –  Otto Jun 28 '11 at 16:57

The files you are trying to include are just small chunks of WordPress core and won't work without being properly loaded by it. You need to load it properly, see integrating WordPress in Codex.

share|improve this answer

Your includes are somewhat incorrect, and also you're missing some other key information that makes up a post (but maybe not a page). On the whole, you're on the right track.

Check out the script jwz created (and I helped him fix up) to see how to make a post via command line. You can extend the idea to make pages too:

share|improve this answer

Maybe I am off, but what are you needing this cron script for? I know you can set a publish date for pages/posts so you can schedule it to post on a certain day/time.

share|improve this answer

I use a modified version of the txt-as-post plugin. With it you can read in a zip of posts. I then call it via cron (I prefer that way than using WPs built in cron).

share|improve this answer

Your Answer

