WordPress.org

Make WordPress Core

Ticket #17379: export-with-attachments.diff

File export-with-attachments.diff, 1.8 KB (added by Ganon, 6 years ago)

Export with attachments and thumbnails

  • wp-admin/includes/export.php

    diff --git a/wp-admin/includes/export.php b/wp-admin/includes/export.php
    index 397e9e7..f3b7a48 100644
    a b function export_wp( $args = array() ) { 
    2929                'start_date' => false, 'end_date' => false, 'status' => false,
    3030        );
    3131        $args = wp_parse_args( $args, $defaults );
     32        $get_attachments = false;
    3233
    3334        /**
    3435         * Fires at the beginning of an export, before any headers are sent.
    function export_wp( $args = array() ) { 
    5354                        $args['content'] = 'post';
    5455
    5556                $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
     57                $get_attachments = true;
    5658        } else {
    5759                $post_types = get_post_types( array( 'can_export' => true ) );
    5860                $esses = array_fill( 0, count($post_types), '%s' );
    function export_wp( $args = array() ) { 
    8688        // Grab a snapshot of post IDs, just in case it changes during the export.
    8789        $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
    8890
     91        if ( $get_attachments ) {
     92                $attachments_parents = $post_ids;
     93
     94                // fetch attachments, 20 posts at a time
     95                while ( $next_posts = array_splice( $attachments_parents, 0, 20 ) ) {
     96                        $attachment_ids = $wpdb->get_col( "SELECT ID
     97                                FROM {$wpdb->posts}
     98                                WHERE {$wpdb->posts}.post_type ='attachment'
     99                                AND {$wpdb->posts}.post_parent IN (".implode(',', $post_ids).")" );
     100
     101                        $thumbnails_ids = $wpdb->get_col( "SELECT meta_value
     102                                FROM {$wpdb->postmeta}
     103                                WHERE {$wpdb->postmeta}.post_id IN (".implode(',', $post_ids).")
     104                                AND {$wpdb->postmeta}.meta_key = '_thumbnail_id'" );
     105
     106                        $post_ids = array_merge($post_ids, $attachment_ids, $thumbnails_ids);
     107                }
     108
     109                // remove posible duplicated ids
     110                $post_ids = array_unique($post_ids);
     111        }
     112
    89113        /*
    90114         * Get the requested terms ready, empty unless posts filtered by category
    91115         * or all content.