Index: src/wp-admin/includes/export.php
===================================================================
--- src/wp-admin/includes/export.php	(revision 31090)
+++ src/wp-admin/includes/export.php	(working copy)
@@ -47,44 +47,80 @@ function export_wp( $args = array() ) {
 	header( 'Content-Disposition: attachment; filename=' . $filename );
 	header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
 
-	if ( 'all' != $args['content'] && post_type_exists( $args['content'] ) ) {
-		$ptype = get_post_type_object( $args['content'] );
-		if ( ! $ptype->can_export )
-			$args['content'] = 'post';
+	$query_args = array(
+		'post_status' => 'any',
+		'posts_per_page' => -1
+	);
+
+	if ( 'all' != $args['content'] ) {
+		$args['content'] = (array) $args['content'];
+
+		$query_args['post_type'] = array();
+
+		foreach( $args['content'] as $post_type ) {
+			$post_type_object = get_post_type_object( $post_type );
+
+			if ( $post_type_object && $post_type_object->can_export ) {
+				$query_args['post_type'][] = $post_type;
+			}
+		}
 
-		$where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
+		if ( empty( $query_args['post_type'] ) ) {
+			$query_args['post_type'] = 'post';
+		}
 	} else {
-		$post_types = get_post_types( array( 'can_export' => true ) );
-		$esses = array_fill( 0, count($post_types), '%s' );
-		$where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types );
+		$query_args['post_type'] = array_values( get_post_types( array( 'can_export' => true ) ) );
 	}
 
-	if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) )
-		$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );
-	else
-		$where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
+	if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) ) {
+		$query_args['post_status'] = $args['status'];
+	}
 
-	$join = '';
 	if ( $args['category'] && 'post' == $args['content'] ) {
 		if ( $term = term_exists( $args['category'], 'category' ) ) {
-			$join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
-			$where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] );
+			$query_args['tax_query'] = array(
+				array(
+					'taxonomy' => 'category',
+					'terms' => $term->term_id,
+					'include_children' => false
+				)
+			);
 		}
 	}
 
 	if ( 'post' == $args['content'] || 'page' == $args['content'] ) {
 		if ( $args['author'] )
-			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
+			$query_args['author'] = (int) $args['author'];
+
+		$date_query = array();
 
 		if ( $args['start_date'] )
-			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) );
+			$date_query[]['after'] = $args['start_date'];
 
-		if ( $args['end_date'] )
-			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime('+1 month', strtotime($args['end_date'])) ) );
+		if ( $args['end_date'] ) {
+			$date_query[]['before'] =  '@' . strtotime( '+1 month', strtotime( $args['end_date'] ) );
+		}
+
+		if ( ! empty( $date_query ) ) {
+			$query_args['date_query'] = $date_query;
+		}
 	}
 
-	// Grab a snapshot of post IDs, just in case it changes during the export.
-	$post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
+	/**
+	 * Filter the query arguments for the exported data
+	 *
+	 * @todo Update since
+	 * @since 4.X
+	 *
+	 * @param array $query_args WP_Query args to use for the exported data.
+	 * @param array $args Filters defining what should be included in the export.
+	 */
+	$query_args = apply_filters( 'export_wp_query_args', $query_args, $args );
+
+	$post_query_args = $query_args;
+	$post_query_args['fields'] = 'ids';
+
+	$post_ids = get_posts( $post_query_args );
 
 	/*
 	 * Get the requested terms ready, empty unless posts filtered by category
@@ -95,29 +131,36 @@ function export_wp( $args = array() ) {
 		$cat = get_term( $term['term_id'], 'category' );
 		$cats = array( $cat->term_id => $cat );
 		unset( $term, $cat );
-	} elseif ( 'all' == $args['content'] ) {
+	} else if ( 'all' == $args['content'] ) {
 		$categories = (array) get_categories( array( 'get' => 'all' ) );
 		$tags = (array) get_tags( array( 'get' => 'all' ) );
 
 		$custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
 		$custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
 
-		// Put categories in order with no child going before its parent.
-		while ( $cat = array_shift( $categories ) ) {
-			if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
-				$cats[$cat->term_id] = $cat;
-			else
-				$categories[] = $cat;
+		// Put categories in order with no child going before its parent
+		if( $categories ) {
+			while ( $cat = array_shift( $categories ) ) {
+				if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) ) {
+					$cats[$cat->term_id] = $cat;
+				} else {
+					$categories[] = $cat;
+				}
+			}
 		}
 
-		// Put terms in order with no child going before its parent.
-		while ( $t = array_shift( $custom_terms ) ) {
-			if ( $t->parent == 0 || isset( $terms[$t->parent] ) )
-				$terms[$t->term_id] = $t;
-			else
-				$custom_terms[] = $t;
+		// Put terms in order with no child going before its parent
+		if( $custom_terms ) {
+			while ( $t = array_shift( $custom_terms ) ) {
+				if ( $t->parent == 0 || isset( $terms[$t->parent] ) ) {
+					$terms[$t->term_id] = $t;
+				} else {
+					$custom_terms[] = $t;
+				}
+			}
 		}
 
+		// Clean up
 		unset( $categories, $custom_taxonomies, $custom_terms );
 	}
 
@@ -375,20 +418,18 @@ function wxr_filter_postmeta( $return_me, $meta_key ) {
 	?>
 
 <?php if ( $post_ids ) {
-	global $wp_query;
+	$query_args['posts_per_page'] = 20;
+	$query_args['paged'] = 1;
 
-	// Fake being in the loop.
-	$wp_query->in_the_loop = true;
+	$query = new WP_Query( $query_args );
 
-	// Fetch 20 posts at a time rather than loading the entire table into memory.
-	while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
-	$where = 'WHERE ID IN (' . join( ',', $next_posts ) . ')';
-	$posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
+	// Paginate posts 20 at a time
+	while ( $query->have_posts() ) {
+		// Begin Loop.
+		while ( $query->have_posts() ) {
+			$query->the_post();
 
-	// Begin Loop.
-	foreach ( $posts as $post ) {
-		setup_postdata( $post );
-		$is_sticky = is_sticky( $post->ID ) ? 1 : 0;
+			$is_sticky = is_sticky( $post->ID ) ? 1 : 0;
 ?>
 	<item>
 		<title><?php
@@ -501,7 +542,12 @@ function wxr_filter_postmeta( $return_me, $meta_key ) {
 <?php	endforeach; ?>
 	</item>
 <?php
-	}
+		}
+
+		// Fetch next page of posts
+		$query_args['paged']++;
+
+		$query->query( $query_args );
 	}
 } ?>
 </channel>
