Skip to content

Commit 8db4b46

Browse files
Export: Split the query for post authors in wxr_authors_list() into smaller chunks.
This aims to avoid a fatal error when attempting to export content on larger WP instances with a lot of data. Follow-up to [15961], [28731]. Props bor0, SirLouen, SergeyBiryukov. Fixes #63503. git-svn-id: https://develop.svn.wordpress.org/trunk@60632 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 94cab03 commit 8db4b46

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/wp-admin/includes/export.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,19 +405,26 @@ function wxr_authors_list( ?array $post_ids = null ) {
405405
global $wpdb;
406406

407407
if ( ! empty( $post_ids ) ) {
408-
$post_ids = array_map( 'absint', $post_ids );
409-
$and = 'AND ID IN ( ' . implode( ', ', $post_ids ) . ')';
408+
$post_ids = array_map( 'absint', $post_ids );
409+
$post_id_chunks = array_chunk( $post_ids, 20 );
410410
} else {
411-
$and = '';
411+
$post_id_chunks = array( array() );
412412
}
413413

414414
$authors = array();
415-
$results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft' $and" );
416-
foreach ( (array) $results as $result ) {
417-
$authors[] = get_userdata( $result->post_author );
415+
416+
foreach ( $post_id_chunks as $next_posts ) {
417+
$and = ! empty( $next_posts ) ? 'AND ID IN (' . implode( ', ', $next_posts ) . ')' : '';
418+
419+
$results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft' $and" );
420+
421+
foreach ( (array) $results as $result ) {
422+
$authors[] = get_userdata( $result->post_author );
423+
}
418424
}
419425

420426
$authors = array_filter( $authors );
427+
$authors = array_unique( $authors, SORT_REGULAR ); // Remove duplicate authors.
421428

422429
foreach ( $authors as $author ) {
423430
echo "\t<wp:author>";

0 commit comments

Comments
 (0)