ap_set_media_post_parent( integer|array $media_id, integer $post_parent, false|int $user_id = false )

Description #

Set parent post for an attachment.

Parameters #

  • $media_id
    integer | array (Required) Attachment ID.
  • $post_parent
    integer (Required) Post parent id.
  • $user_id
    false | int (Optional) User id. Default value: false

Source #

File: includes/upload.php

function ap_set_media_post_parent( $media_id, $post_parent, $user_id = false ) {
	if ( ! is_array( $media_id ) ) {
		$media_id = array( $media_id );
	}

	if ( false === $user_id ) {
		$user_id = get_current_user_id();
	}

	foreach ( (array) $media_id as $id ) {
		$attach = get_post( $id );

		if ( $attach && 'attachment' === $attach->post_type && $user_id == $attach->post_author ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
			$postarr = array(
				'ID'          => $attach->ID,
				'post_parent' => $post_parent,
				'post_title'  => preg_replace( '/\.[^.]+$/', '', basename( $attach->guid ) ),
			);

			wp_update_post( $postarr );
		}
	}

	ap_update_post_attach_ids( $post_parent );
	ap_update_user_temp_media_count( $user_id );
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment