WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Inserting post from outside wp - works, kind of ... help? (5 posts)

  1. gypmaster
    Member
    Posted 5 years ago #

    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?

  2. gypmaster
    Member
    Posted 5 years ago #

    anyone?

  3. Michael Fields
    Themer
    Posted 5 years ago #

    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

  4. gypmaster
    Member
    Posted 5 years ago #

    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.

  5. tommix
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic