Inserting WordPress Post via External Script

I’m trying to insert bulk posts via an external php script:

<?php
require_once '/full/path/to/wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';

$title = "some post title";
$content = "some content";
$tags= "tag1,tag2,tag3";
$user_id = 1;


// Create post object
$my_post = array(

   'post_title'    => $title,
   'post_content'  => $content,
   'post_status'   => 'publish',
   'post_author'   => $user_id,
   'post_type'     => 'post',
   'tags_input'    => $tags,
 );

$id = wp_insert_post($my_post,true);
?>

$id returns 0 and WP_Error is empty.
Post is inserted to DB with right title and content but without tags. It also fails to use wp_insert_terms() to insert tags or other custom taxonomies.

Read More

Did i miss a file to include or is there something i didn’t set right to work functions properly?

Related posts

2 comments

  1. you don’t need to load taxonomy.php.. this functionality is handled by wp-load.php

    also i would suggest that you put this on first position in your file:

    define( 'WP_USE_THEMES', false );
    

    than you can use wp_insert_post() to add the post.. but to add the tags or category and so on you need to use

    wp_set_post_terms() 
    

    or

    wp_set_object_terms() 
    

    with your recieved $id from wp_insert_post.

    for more informations check this: https://wordpress.stackexchange.com/questions/18236/attaching-taxonomy-data-to-post-with-wp-insert-post

  2. Please include the wordpress database files first.

    require_once 'wp-load.php';
    require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
    

    and use the following code to insert post dynamically from the external script.

     $newIds = wp_insert_post( array(
                'post_title' => $postCSVContent['1'],
                'post_content' => $postCSVContent['2'],
                'post_type' => 'doors',
                'post_status' => 'publish',        
                'post_author'   => 1,
                'post_parent' => $parentId
        ));
    

    Refer the tutorial that explains how to create and update wordpress post programmatically.

    http://www.pearlbells.co.uk/insert-udpate-wordpress-post-programmatically/

Comments are closed.