Если у Вас предполагается большое количество записей, постов (а для сео это надо), то необходимо добавить небольшой код, чтобы сохранить медиатеку wordpress в чистоте!
// Удалять медиа при удалении поста
add_action( 'before_delete_post', function( $id ) {
$attachments = get_attached_media( '', $id );
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID, true );
}
} );Если у Вас добавление записей сделано через фронтед в форме jetformbuilder. То добавьте бесплатный мини-плагин Attach Media Post https://crocoblock.com/freemium/tools/ или просто код плагина добавить в дочернюю тему.

Код плагина, чтобы не устанавливать целый плагин 😇
// Удалять медиа с фронтеда при удалении записи
use Jet_Form_Builder\Actions\Action_Handler;
if ( ! defined( 'WPINC' ) ) {
die();
}
add_action( 'jet-engine/forms/booking/notification/insert_post', 'jet_forms_attach_media_to_post', 20, 2 );
$jfb_inserted_attachments = array();
add_action( 'jet-form-builder/action/after-post-insert', 'jet_forms_attach_media_to_post_jfb', 20 );
add_action( 'jet-form-builder/action/after-post-update', 'jet_forms_attach_media_to_post_jfb', 20 );
add_action( 'jet-form-builder/inserted-attachment', function ( $file, $uploader ) {
global $jfb_inserted_attachments;
/** @var \Jet_Form_Builder\Request\File_Uploader $uploader */
/** @var \Jet_Form_Builder\Classes\Resources\Uploaded_File $file */
$name = $uploader->get_settings()['name'] ?? '';
if ( ! isset( $jfb_inserted_attachments[ $name ] ) ) {
$jfb_inserted_attachments[ $name ] = array();
}
$jfb_inserted_attachments[ $name ][] = $file->get_attachment_id();
}, 10, 2 );
function _jet_forms_attach_images( $request_source, $media_keys, $inserted_post_id ) {
$get_attachment_image_data_array = function ( $img_data = null, $include = 'all' ) {
$result = false;
if ( empty( $img_data ) ) {
return $result;
}
if ( is_numeric( $img_data ) ) {
switch ( $include ) {
case 'id':
$result = array(
'id' => $img_data,
);
break;
case 'url':
$result = array(
'url' => wp_get_attachment_url( $img_data ),
);
break;
default:
$result = array(
'id' => $img_data,
'url' => wp_get_attachment_url( $img_data ),
);
}
} elseif ( filter_var( $img_data, FILTER_VALIDATE_URL ) ) {
switch ( $include ) {
case 'id':
$result = array(
'id' => attachment_url_to_postid( $img_data ),
);
break;
case 'url':
$result = array(
'url' => $img_data,
);
break;
default:
$result = array(
'id' => attachment_url_to_postid( $img_data ),
'url' => $img_data,
);
}
} elseif ( is_array( $img_data ) && isset( $img_data['id'] ) && isset( $img_data['url'] ) ) {
switch ( $include ) {
case 'id':
$result = array(
'id' => $img_data['id'],
);
break;
case 'url':
$result = array(
'url' => $img_data['url'],
);
break;
default:
$result = $img_data;
}
}
return $result;
};
foreach ( $media_keys as $media_key ) {
if ( empty( $request_source[ $media_key ] ) ) {
continue;
}
$attachment_data = $request_source[ $media_key ];
if ( ! is_array( $attachment_data ) ) {
$attachment_data = explode( ',', $request_source[ $media_key ] );
}
$attachment_data = isset( $attachment_data['id'] ) ? array( $attachment_data ) : $attachment_data;
$attachment_ids = array_map( function ( $item ) use ( $get_attachment_image_data_array ) {
return call_user_func( $get_attachment_image_data_array, $item, 'id' );
}, $attachment_data );
$attachment_ids = wp_list_pluck( $attachment_ids, 'id' );
foreach ( $attachment_ids as $attachment_id ) {
wp_update_post( array(
'ID' => $attachment_id,
'post_parent' => $inserted_post_id,
) );
}
}
}
function jet_forms_attach_media_to_post_jfb() {
global $jfb_inserted_attachments;
$inserted_id = jet_fb_action_handler()->get_inserted_post_id();
if ( empty( $inserted_id ) ) {
return;
}
foreach ( $jfb_inserted_attachments as $attachment_ids ) {
foreach ( $attachment_ids as $id ) {
wp_update_post( array(
'ID' => $id,
'post_parent' => $inserted_id,
) );
}
}
}
function jet_forms_attach_media_to_post( $notification, $manager ) {
if ( empty( $manager->data['inserted_post_id'] ) ) {
return;
}
$form_id = absint( $manager->form );
$form_data = get_post_meta( $form_id, '_form_data', true );
$form_data = json_decode( wp_unslash( $form_data ), true );
if ( empty( $form_data ) ) {
return;
}
$field_settings = wp_list_pluck( $form_data, 'settings' );
$media_keys = array();
foreach ( $field_settings as $field_setting ) {
if ( ! empty( $field_setting['type'] ) && 'media' === $field_setting['type']
&& isset( $field_setting['insert_attachment'] )
&& filter_var( $field_setting['insert_attachment'], FILTER_VALIDATE_BOOLEAN )
) {
$media_keys[] = $field_setting['name'];
}
}
if ( empty( $media_keys ) ) {
return;
}
call_user_func( '_jet_forms_attach_images', $manager->data, $media_keys, $manager->data['inserted_post_id'] );
}Теперь при удалении записи будут удаляться и прикреплённые медиа. Это очень удобно!