post_author for wp_insert_attachment

I am working on a custom theme, in which I need to programmatically inserts posts and attachments. One requirement is those posts belong to users that are not the user who is logged in and running the action.

To give you an example, an admin user logs in to WordPress, and runs this custom action. The custom action will create suggested posts for two other users, user1 and user2.

Read More

Now if user1 logs in, he/she will see a suggested post and he can choose to publish it.

Everything is cool and dandy so far, because the function wp_insert_post allows for programattic insertion of posts. One of the arguments that this function takes is post_author which can be set to the id of either user1 or user2.

So far, everything is still great.

Now, those post will have attachments (images). My question is, is there a way to add a post_author to the wp_insert_attachment? I could not find that in the documentation. I need the attachment to have the same author id as that of the post. Currently it takes the id of the the admin.

Thanks.

Related posts

1 comment

  1. My question is, is there a way to add a post_author to the
    wp_insert_attachment? I could not find that in the documentation. I
    need the attachment to have the same author id as that of the post.
    Currently it takes the id of the the admin.

    Sometimes the codex or published docs don’t tell the whole story. It’s always better if you don’t understand how a function works to go directly to the source. If you look at the PHP Doc block for wp_insert_attachment() You will find the answer you are looking for.

    /**
     * Insert an attachment.
     *
     * If you set the 'ID' in the $object parameter, it will mean that you are
     * updating and attempt to update the attachment. You can also set the
     * attachment name or title by setting the key 'post_name' or 'post_title'.
     *
     * You can set the dates for the attachment manually by setting the 'post_date'
     * and 'post_date_gmt' keys' values.
     *
     * By default, the comments will use the default settings for whether the
     * comments are allowed. You can close them manually or keep them open by
     * setting the value for the 'comment_status' key.
     *
     * The $object parameter can have the following:
     *     'post_status'   - Default is 'draft'. Can not be overridden, set the same as parent post.
     *     'post_type'     - Default is 'post', will be set to attachment. Can not override.
     *     'post_author'   - Default is current user ID. The ID of the user, who added the attachment.
     *     'ping_status'   - Default is the value in default ping status option. Whether the attachment
     *                       can accept pings.
     *     'post_parent'   - Default is 0. Can use $parent parameter or set this for the post it belongs
     *                       to, if any.
     *     'menu_order'    - Default is 0. The order it is displayed.
     *     'to_ping'       - Whether to ping.
     *     'pinged'        - Default is empty string.
     *     'post_password' - Default is empty string. The password to access the attachment.
     *     'guid'          - Global Unique ID for referencing the attachment.
     *     'post_content_filtered' - Attachment post content filtered.
     *     'post_excerpt'  - Attachment excerpt.
     *
     * @since 2.0.0
     * @uses $wpdb
     * @uses $user_ID
     * @uses do_action() Calls 'edit_attachment' on $post_ID if this is an update.
     * @uses do_action() Calls 'add_attachment' on $post_ID if this is not an update.
     *
     * @param string|array $object Arguments to override defaults.
     * @param string $file Optional filename.
     * @param int $parent Parent post ID.
     * @return int Attachment ID.
     */
    

Comments are closed.