wp_xmlrpc_server::attach_uploads()publicWP 2.1.0

Attaches an upload to a post.

Method of the class: wp_xmlrpc_server{}

No Hooks.

Return

null. Nothing (null).

Usage

$wp_xmlrpc_server = new wp_xmlrpc_server();
$wp_xmlrpc_server->attach_uploads( $post_id, $post_content );
$post_id(int) (required)
Post ID.
$post_content(string) (required)
Post Content for attachment.

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 2.1.0 Introduced.

wp_xmlrpc_server::attach_uploads() code WP 6.4.3

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 ) );
			}
		}
	}
}