@@ -34,6 +34,20 @@ class WP_Export_Query {
3434
3535 public $ missing_parents = false ;
3636
37+ /**
38+ * Terms, meta and comments of the posts in the current batch, keyed by post ID.
39+ *
40+ * @var array<int, array{terms: array<\WP_Term>, meta: array<object>, comments: array<object>}>
41+ */
42+ private $ batch_data = [];
43+
44+ /**
45+ * Positions of the post IDs in $post_ids, to find the batch a post belongs to.
46+ *
47+ * @var array<int, int>|null
48+ */
49+ private $ post_id_positions ;
50+
3751 public function __construct ( $ filters = [] ) {
3852 $ this ->filters = wp_parse_args ( $ filters , self ::$ defaults );
3953
@@ -156,9 +170,21 @@ public function exportify_post( $post ) {
156170 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
157171 $ post ->post_excerpt = apply_filters ( 'the_excerpt_export ' , $ post ->post_excerpt );
158172 $ post ->is_sticky = is_sticky ( $ post ->ID ) ? 1 : 0 ;
159- $ post ->terms = self ::get_terms_for_post ( $ post );
160- $ post ->meta = self ::get_meta_for_post ( $ post );
161- $ post ->comments = $ this ->get_comments_for_post ( $ post );
173+
174+ if ( ! isset ( $ this ->batch_data [ $ post ->ID ] ) ) {
175+ $ this ->load_batch_data ( $ post ->ID );
176+ }
177+
178+ if ( isset ( $ this ->batch_data [ $ post ->ID ] ) ) {
179+ $ post ->terms = $ this ->batch_data [ $ post ->ID ]['terms ' ];
180+ $ post ->meta = $ this ->batch_data [ $ post ->ID ]['meta ' ];
181+ $ post ->comments = $ this ->batch_data [ $ post ->ID ]['comments ' ];
182+ unset( $ this ->batch_data [ $ post ->ID ] );
183+ } else {
184+ $ post ->terms = self ::get_terms_for_post ( $ post );
185+ $ post ->meta = self ::get_meta_for_post ( $ post );
186+ $ post ->comments = $ this ->get_comments_for_post ( $ post );
187+ }
162188 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Revert back to original.
163189 $ GLOBALS ['post ' ] = $ previous_global_post ;
164190 return $ post ;
@@ -413,6 +439,114 @@ private function process_orphaned_terms( $terms ) {
413439 return $ terms ;
414440 }
415441
442+ /**
443+ * Load the terms, meta and comments of the batch of posts starting with the given one.
444+ *
445+ * Querying them for a whole batch at once avoids several queries per exported post.
446+ * The data is the same as what get_terms_for_post(), get_meta_for_post() and
447+ * get_comments_for_post() return for each post.
448+ *
449+ * @param int $post_id ID of the first post of the batch.
450+ */
451+ private function load_batch_data ( $ post_id ) {
452+ global $ wpdb ;
453+
454+ if ( null === $ this ->post_id_positions ) {
455+ $ this ->post_id_positions = array_flip ( array_map ( 'intval ' , $ this ->post_ids ) );
456+ }
457+
458+ $ this ->batch_data = [];
459+ if ( ! isset ( $ this ->post_id_positions [ $ post_id ] ) ) {
460+ return ;
461+ }
462+
463+ $ post_ids = array_map ( 'intval ' , array_slice ( $ this ->post_ids , $ this ->post_id_positions [ $ post_id ], self ::QUERY_CHUNK ) );
464+ $ in_ids = implode ( ', ' , $ post_ids );
465+
466+ $ terms_by_post = array_fill_keys ( $ post_ids , [] );
467+ $ meta_by_post = array_fill_keys ( $ post_ids , [] );
468+ $ comments_by_post = array_fill_keys ( $ post_ids , [] );
469+
470+ // Terms, queried per post type as the taxonomies depend on it.
471+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- integers only
472+ $ post_types = $ wpdb ->get_results ( "SELECT ID, post_type FROM $ wpdb ->posts WHERE ID IN ( $ in_ids) " );
473+ $ ids_by_type = [];
474+ foreach ( $ post_types as $ row ) {
475+ $ ids_by_type [ $ row ->post_type ][] = (int ) $ row ->ID ;
476+ }
477+ foreach ( $ ids_by_type as $ post_type => $ ids ) {
478+ $ taxonomies = get_object_taxonomies ( $ post_type );
479+ if ( empty ( $ taxonomies ) ) {
480+ continue ;
481+ }
482+ $ terms = wp_get_object_terms ( $ ids , $ taxonomies , [ 'fields ' => 'all_with_object_id ' ] );
483+ if ( is_wp_error ( $ terms ) ) {
484+ continue ;
485+ }
486+ foreach ( $ terms as $ term ) {
487+ if ( ! $ term instanceof \WP_Term ) {
488+ continue ;
489+ }
490+ // Set by wp_get_object_terms() for 'all_with_object_id'.
491+ $ object_id = (int ) $ term ->object_id ; // @phpstan-ignore property.notFound
492+ if ( isset ( $ terms_by_post [ $ object_id ] ) ) {
493+ $ terms_by_post [ $ object_id ][] = $ term ;
494+ }
495+ }
496+ }
497+
498+ // Terms are ordered by name. Order terms with the same name, e.g. a category and a tag,
499+ // by their term_taxonomy_id like the query for a single post returns them.
500+ foreach ( $ terms_by_post as &$ terms ) {
501+ usort (
502+ $ terms ,
503+ static function ( $ a , $ b ) {
504+ return [ $ a ->name , $ a ->term_taxonomy_id ] <=> [ $ b ->name , $ b ->term_taxonomy_id ];
505+ }
506+ );
507+ }
508+ unset( $ terms );
509+
510+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- integers only
511+ $ meta_from_db = $ wpdb ->get_results ( "SELECT * FROM $ wpdb ->postmeta WHERE post_id IN ( $ in_ids) ORDER BY post_id, meta_id " );
512+ foreach ( $ meta_from_db as $ meta ) {
513+ if ( '_edit_lock ' === $ meta ->meta_key ) {
514+ continue ;
515+ }
516+ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
517+ if ( apply_filters ( 'wxr_export_skip_postmeta ' , false , $ meta ->meta_key , $ meta ) ) {
518+ continue ;
519+ }
520+ $ meta_by_post [ (int ) $ meta ->post_id ][] = $ meta ;
521+ }
522+
523+ if ( ! isset ( $ this ->filters ['skip_comments ' ] ) ) {
524+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- integers only
525+ $ comments = $ wpdb ->get_results ( "SELECT * FROM $ wpdb ->comments WHERE comment_post_ID IN ( $ in_ids) AND comment_approved <> 'spam' ORDER BY comment_post_ID, comment_ID " );
526+ if ( ! empty ( $ comments ) ) {
527+ $ comment_meta = [];
528+ $ comment_ids = implode ( ', ' , array_map ( 'intval ' , wp_list_pluck ( $ comments , 'comment_ID ' ) ) );
529+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- integers only
530+ foreach ( $ wpdb ->get_results ( "SELECT * FROM $ wpdb ->commentmeta WHERE comment_id IN ( $ comment_ids) ORDER BY comment_id, meta_id " ) as $ meta ) {
531+ $ comment_meta [ (int ) $ meta ->comment_id ][] = $ meta ;
532+ }
533+
534+ foreach ( $ comments as $ comment ) {
535+ $ comment ->meta = isset ( $ comment_meta [ (int ) $ comment ->comment_ID ] ) ? $ comment_meta [ (int ) $ comment ->comment_ID ] : [];
536+ $ comments_by_post [ (int ) $ comment ->comment_post_ID ][] = $ comment ;
537+ }
538+ }
539+ }
540+
541+ foreach ( $ post_ids as $ id ) {
542+ $ this ->batch_data [ $ id ] = [
543+ 'terms ' => $ terms_by_post [ $ id ],
544+ 'meta ' => $ meta_by_post [ $ id ],
545+ 'comments ' => $ comments_by_post [ $ id ],
546+ ];
547+ }
548+ }
549+
416550 private static function get_terms_for_post ( $ post ) {
417551 $ taxonomies = get_object_taxonomies ( $ post ->post_type );
418552 if ( empty ( $ taxonomies ) ) {
0 commit comments