WordPress.org

Ready to get started?Download WordPress

Forums

wp_insert_post categories array with variables (3 posts)

  1. IshidaM
    Member
    Posted 2 years ago #

    This code works:

    $post_id = wp_insert_post( array(
    		'post_author'	=> $user_id,
                    'post_category' => array(3) )

    //some stuff omitted

    This does not

    $post_id = wp_insert_post( array(
    		'post_author'	=> $user_id,
                    'post_category' => array($category_id) )

    Using echo, $category_id is indeed 3, so I don't get why this is not working. I'm guessing this has something to do with php and array creation but I couldn't figure out what is wrong.

  2. IshidaM
    Member
    Posted 2 years ago #

    Anyone know whats wrong with the php code?

  3. marsh_lion
    Member
    Posted 2 years ago #

    The PHP code you have written is creating an array with 3 elements in it, not an array with 1 element of 3.

    PS. Yes I know this is an old query - I happened to stumble on it when searching for my issue with categories (and is something I find a pain as well)

Topic Closed

This topic has been closed to new replies.

About this Topic