Attaches an upload to a post.
Parameters
$post_id
intrequired- Post ID.
$post_content
stringrequired- Post Content for attachment.
Source
public function attach_uploads( $post_id, $post_content ) {
global $wpdb;
// Find any unattached files.
$attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
if ( is_array( $attachments ) ) {
foreach ( $attachments as $file ) {
if ( ! empty( $file->guid ) && str_contains( $post_content, $file->guid ) ) {
$wpdb->update( $wpdb->posts, array( 'post_parent' => $post_id ), array( 'ID' => $file->ID ) );
}
}
}
}
Changelog
Version | Description |
---|---|
2.1.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.