wp_xmlrpc_server::attach_uploads( int $post_id, string $post_content )

In this article

Attaches an upload to a post.

Parameters

$post_idintrequired
Post ID.
$post_contentstringrequired
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

VersionDescription
2.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.