• I try toi use this code, but don’t work:

    // create object class
    class wm_mypost {

    var $post_title;
    var $post_content;
    var $post_categories;
    var $post_status;
    var $post_author;
    }
    //// my first object :-)
    $wm_myobject = new wm_mypost();
    // fill object
    $wm_myobject->post_title = $title;
    $wm_myobject->post_content = $content;
    $wm_myobject->post_categories = $id_category;
    $wm_myobject->post_status = 'publish';
    $wm_myobject->post_author = $id_author;
    // feed object to wp_insert_post
    wp_insert_post($wm_myobject);

    Please help me

Viewing 11 replies - 1 through 11 (of 11 total)
  • Reus did you ever get to the bottom of this? I too am trying to insert a post into a specific category, and it always gos into uncategorized.

    I saw this example online somewhere too, but if you look at the code for wp_insert_post, it takes an array as input, not some class. This might work the way it’s written, but the error is that the post_category (not categories, category) field must be an array of strings, not a string.

    If you fix the name and change the string to an array, maybe it will work. I would tell you a better way to do it, but I haven’t got it working yet myself…

    Aha! My error was because I was inserting the post to a category that didn’t exist. I need to figure out how to make a category now…

    Ok. It works and here’s the code:

    wp_create_category($cat_name); 	// Creates the category (if it doesn't already exist)

    After that, change the Post->categories to post->category and it will insert correctly. Now as to how to make it actually set the inserted post to the right category… I haven’t actually solved that yet.

    Nope that did it. Make sure the category exists and change to category from categories. It works perfectly for me now. YAY!

    the full use of wp_create_categories is

    wp_create_categories ($category_name, $post_id)

    In this case, it will create a new category in the case the one you are trying to assing do not exist.

    The problem is that this is theory and do not really works. Not for me.

    Thank you for this post!

    jeremyduffy, can you post your code for this? did you end up having to insert the category as an array? How did you pass it in? a form? Thanks!

    Doh. Sorry for not checking back on this thread, but WordPress doesn’t alert you to updates in a thread like OTHER SITES DO (/end rant).

    I’ve posted the code that works here:

    http://www.jeremyduffy.com/computers-internet/wordpress-stuff/manually-insert-pagepost-into-wordpress-via-php/

    Similar code can be found on the Codex, the page about wp_insert_post.

    What Are You Guys Talking About

    Nicholas Marquiess

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘does “wp_insert_post” insert post with a specific category ???’ is closed to new replies.