1

WordPress create new post on bulk image upload

WordPress create new post on bulk image upload. This is custom function for creating one WordPress post for every one image, The post will be created with the same name as image file name, and then image is added to the post as thumbnail.

Code for WordPress create new post on bulk image upload.

Add this code in the theme functions.php file.

add_action('add_attachment', 'create_post');
function create_post( $attach_ID ) {

    $attachment = get_post( $attach_ID );

    $my_post_data = array(
                'post_title' => $attachment->post_title,
                'post_type' => 'post',
                'post_category' => array('0'),
                'post_status' => 'publish'
    );
    $post_id = wp_insert_post( $my_post_data );

    // attach media to post
    wp_update_post( array(
        'ID' => $attach_ID,
        'post_parent' => $post_id,
    ) );

    set_post_thumbnail( $post_id, $attach_ID );

    return $attach_ID;
}

Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 1 comments
ryo

hi mate!
i am using this code, but it automatically create thumbnail, how if i want to change it as featured image?

Reply

Leave a Reply:

x

Join My Facebook Group to Receive Coding Tutorial for Free

email-subsribe