Skip to content

Commit 9054f7e

Browse files
Copilotswissspidy
andcommitted
Simplify term retrieval with cache clearing
Reverted to simpler approach using wp_get_object_terms() with proper error handling. Added clean_post_cache() call to ensure fresh term data is retrieved, avoiding any stale cache issues. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 67b15b2 commit 9054f7e

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

src/WP_Export_Query.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ public function nav_menu_terms() {
137137
}
138138

139139
public function exportify_post( $post ) {
140-
// Ensure we have a proper WP_Post object.
141-
$post = get_post( $post->ID );
142140
$GLOBALS['wp_query']->in_the_loop = true;
143141
$previous_global_post = Utils\get_flag_value( $GLOBALS, 'post' );
144142
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Temporary override.
@@ -404,23 +402,25 @@ private function process_orphaned_terms( $terms ) {
404402
}
405403

406404
private static function get_terms_for_post( $post ) {
405+
// Ensure post_type is set.
406+
if ( empty( $post->post_type ) ) {
407+
return [];
408+
}
409+
407410
$taxonomies = get_object_taxonomies( $post->post_type );
408411
if ( empty( $taxonomies ) ) {
409412
return [];
410413
}
411414

412-
// Prime the term cache for this post to ensure term relationships are loaded.
413-
update_object_term_cache( $post->ID, $post->post_type );
415+
// Clear any stale cache and fetch fresh term data.
416+
clean_post_cache( $post->ID );
417+
$terms = wp_get_object_terms( $post->ID, $taxonomies );
414418

415-
$terms = [];
416-
foreach ( $taxonomies as $taxonomy ) {
417-
$tax_terms = get_the_terms( $post->ID, $taxonomy );
418-
if ( $tax_terms && ! is_wp_error( $tax_terms ) ) {
419-
$terms = array_merge( $terms, $tax_terms );
420-
}
419+
if ( is_wp_error( $terms ) ) {
420+
return [];
421421
}
422422

423-
return $terms;
423+
return is_array( $terms ) ? $terms : [];
424424
}
425425

426426
private static function get_meta_for_post( $post ) {

0 commit comments

Comments
 (0)