• Resolved gypmaster

    (@gypmaster)


    Hi,

    running the following code:

    <?php
    require('wp-blog-header.php');
    
    // Create post object
      $my_post = array();
      $my_post['post_title'] = 'My post';
      $my_post['post_content'] = 'This is my next post.';
      $my_post['post_status'] = 'publish';
      $my_post['post_author'] = 1;
      $my_post['post_category'] = array('4');
    
    // Insert the post into the database
      wp_insert_post( $my_post );
    ?>

    which inserts the post into the database … only problem is that the category field in the db is set to 0 and I set it to 4 in the array above.

    anytone know why it’s appearing as 0 and not 4?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter gypmaster

    (@gypmaster)

    anyone?

    As far as I know the post_category database column is no longer in use. WordPress now uses 3 tables to deal with categories:

    wp_terms
    wp_term_relationships
    wp_term_taxonomy

    I used your code exactly as you wrote it and it inserted a new post and (due to the fact that I do not have a category with an ID of 4) it created a new category named 4 with an ID of 68.

    Everything looks good to me with the code in 2.7

    Thread Starter gypmaster

    (@gypmaster)

    yeah, thanks for that … I cans ee it now, and it works fine.

    checking the wp_term_relationships table I can see the post id and the term_taxonomy_id match up the the ‘category’.

    cheers.

    i think it not should be like your: array(‘4’);
    it’s not string so need to be array(4);

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Inserting post from outside wp – works, kind of … help?’ is closed to new replies.