@@ -362,6 +362,7 @@ public function sync_sections_after_rest_save( \WP_Post $post, \WP_REST_Request
362362 }
363363
364364 $ this ->sync_product_meta ( $ post ->ID );
365+ $ this ->sync_section_product_meta ( $ post ->ID );
365366 }
366367
367368 /**
@@ -394,6 +395,7 @@ public function maybe_sync_sections_during_save( $post_id, $post ) {
394395 try {
395396 $ this ->assign_section_terms ( $ post_id , $ section_ids );
396397 $ this ->sync_product_meta ( $ post_id );
398+ $ this ->sync_section_product_meta ( $ post_id );
397399 } finally {
398400 $ this ->is_syncing = false ;
399401 }
@@ -471,6 +473,39 @@ private function sync_product_meta( $post_id ) {
471473 update_post_meta ( $ post_id , '_wzkb_product_ids ' , array_values ( $ product_ids ) );
472474 }
473475
476+ /**
477+ * Backfill product_id term meta on sections that have none.
478+ *
479+ * When exactly one product is assigned to the article, any section that
480+ * lacks a product_id term meta is updated to match. This ensures sections
481+ * linked via the article editor are discoverable by the frontend, which
482+ * queries sections by product_id term meta.
483+ *
484+ * Skips sections that already carry a product_id so that explicit
485+ * assignments made via the Sections admin are never overwritten.
486+ *
487+ * @param int $post_id Post ID.
488+ */
489+ private function sync_section_product_meta ( int $ post_id ): void {
490+ $ product_terms = get_the_terms ( $ post_id , 'wzkb_product ' );
491+ if ( empty ( $ product_terms ) || is_wp_error ( $ product_terms ) || 1 !== count ( $ product_terms ) ) {
492+ return ;
493+ }
494+
495+ $ section_terms = get_the_terms ( $ post_id , 'wzkb_category ' );
496+ if ( empty ( $ section_terms ) || is_wp_error ( $ section_terms ) ) {
497+ return ;
498+ }
499+
500+ $ product_id = (int ) $ product_terms [0 ]->term_id ;
501+
502+ foreach ( $ section_terms as $ section ) {
503+ if ( 0 === (int ) get_term_meta ( $ section ->term_id , 'product_id ' , true ) ) {
504+ update_term_meta ( $ section ->term_id , 'product_id ' , $ product_id );
505+ }
506+ }
507+ }
508+
474509 /**
475510 * Retrieve sanitized IDs from POST input.
476511 *
0 commit comments