• I have a hundreds of pages I’d like to setup in a WordPress site. I’m trying to create a php command-line script I can use to parse through the data and create pages. The data changes regularly, so I need to automate things.

    Obviously, I should use the WP functions instead of directly using the MySQL tables. The following script runs without error…but no page is created ??

    Any pointers on what I’m doing wrong?

    #!/usr/local/php5/bin/php
    <?php
            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_filtered'] = '<h1>Here is my info</h1>\n<ul><li>one</li><li>two</li></ul>\n';
            $to_insert['post_type'] = 'page';
            $to_insert['post_title'] = 'We Must Test';
    
            wp_insert_post($to_insert);
Viewing 6 replies - 1 through 6 (of 6 total)
  • Since you’re not defining ‘post_status’, it might be creating a draft… did you check the admin area?

    You can also try using ‘post_content’ instead of ‘post_content_filtered’…

    Thread Starter raindog308

    (@raindog308)

    No, no joy on changing either of those things. No draft, no nothin’

    I added some code to check the result of the WP_Error returned from wp_insert_post but there’s no error. And nothing in the wp_posts table, either.

    I can’t be the only guy in the world who wants to create WP pages automatically…

    You’re definitely not the only guy — I’m doing it on one of my projects now. This is the code I’m using and it’s working fine (it’s doing a “post” post_type and I’m using variables to designate author, title, and category):

    //-- Set up post values
    		$myPost = array(
    			'post_status' => 'publish',
    			'post_type' => 'post',
    			'post_author' => $authorID,
    			'post_title' => $title,
    			'comment_status' => 'closed',
    			'ping_status' => 'closed',
    			'post_category' => array($category),
    		);
    
    		//-- Create the new post
    		$newPostID = wp_insert_post($myPost);

    Given that our two sets of scripts are extremely similar, my next guess would be that the author_id is invalid and maybe that’s somehow preventing the page from being added. Maybe try testing with an actual author ID?

    Thread Starter raindog308

    (@raindog308)

    Thanks, Cosmonaut. Which WP files are you including before the call to wp_insert_post?

    I’m requiring:

    require('from/your/template/folder/path/to/wp-blog-header.php');

    Which in my case (because I keep my PHP in a subfolder) is:

    require('../../../../wp-blog-header.php');

    Thread Starter raindog308

    (@raindog308)

    Found it – the trick is to include wp-load.php and then everything works. I was including wp-admin.php and wp-post.php, which is not right.

    Thanks, Cosmonaut!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Inserting Posts via External Script’ is closed to new replies.