1, 'post_type' => 'attachment', 'no_found_rows' => 1, 'meta_query' => array( array( 'key' => 'old_url', 'value' => trim( $url ), ), ), ); $get_posts = new WP_Query( $args ); if ( isset( $get_posts->posts[0] ) ) { return $get_posts->posts[0]; } return false; } // Nos articles à insérer $posts = [ [ 'titre' => 'titre 1', 'content' => 'description', 'image_une' => 'http://exemple.com/image.jpg', 'meta' => [ 'extId' => 1 ], ], [ 'titre' => 'titre 2', 'content' => 'description 2', 'image_une' => 'http://exemple.com/image.jpg', 'meta' => [ 'extId' => 2 ], ], ]; // La boucle d’insertion foreach ( $posts as $post ) { $post['post_type'] = 'post'; $post['post_status'] = 'publish'; $meta = $post['meta']; $img = isset( $post['image_une'] ) ? $post['image_une'] : false; unset( $post['meta'] ); unset( $post['image_une'] ); $p = wp_insert_post( $post, true ); if ( is_wp_error( $p ) ) { return false; } if ( $img ) { // On regarde si l’image existe, sinon on l’insère if ( ! $id_attachment = wp_get_attachment_by_post_meta( $img ) ) { $id_attachment = import_image( $img, $img, $p ); } if ( ! is_wp_error( $id_attachment ) ) { $meta['_thumbnail_id'] = $id_attachment; } } if ( ! empty( $meta ) ) { foreach ( $meta as $k => $m ) { update_post_meta( $p, $k, $m ); } } }