• I have a theme that uses custom post type ‘project’.

    I want to bulk upload a load of pictures and have each picture in it’s own ‘project’ with it being the featured image.

    I found this code snippet which I added to functions.php but it seems to not work – nothing happens when I upload an image via the media library. Can anyone suggest a way to achieve this or what’s wrong with this code?

    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' => 'project',
                    '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;
    }
  • The topic ‘Make a new post with featured image for every image uploaded’ is closed to new replies.