I am pulling data from an external web service into a custom post type. This data includes images. How can I create an image gallery, add some existing attachments to it, and associate it with a post?
What I was hoping to find is something like a set_post_gallery
counterpart to the get_post_gallery
function, but I can’t find anything like it in the codex, on google, or in wp-includes/media.php
.
This is how I set up the attachments:
$attachment = [
'guid' => wp_upload_dir()[ 'url' ] . "https://wordpress.stackexchange.com/" . basename( $path ),
'post_mime_type' => wp_check_filetype( basename( $path ), null )[ 'type' ],
'post_title' => "$mlsNum $id",
'post_content' => '',
'post_status' => 'inherit'
];
$attachmentId = wp_insert_attachment( $attachment, $path, $this->postId );
// Generate attachment metadata and create resized images.
wp_update_attachment_metadata( $attachmentId, wp_generate_attachment_metadata( $attachmentId, $path ));
And this is how I am trying to retrieve the gallery for the theme:
$gallery = get_post_gallery( $post, false );
var_dump( $gallery );
var_dump( $post );
$post
is defined, and $gallery
is false. I was under the impression that wp_insert_attachment
would create a gallery for the post and add the attachment to it, but apparently this is not the case. If it was, then that would cause other problems for me when I go to attach a PDF to the post.