Skip to content

Commit e7291f2

Browse files
committed
Recreate missing connect terms if post is published
If a post is published, but somehow does not have an associated _connect term, and the post is updated, recreate the _connect term and re-associate any lost associations.
1 parent 4c8fcdf commit e7291f2

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

includes/sync.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ function sync_shadow_taxonomies( int $post_id, \WP_Post $post_after, bool $updat
8787
return;
8888
}
8989

90+
// If a post is already published and not undergoing a status change, but does not
91+
// have an associated term, create one.
92+
if ( 'publish' === $status_before && 'publish' === $status_after && false === $term_after ) {
93+
// In the very unlikely condition that a term _was_ associated, but now is
94+
// lost to the ether, attempt to restore previous post associations.
95+
$existing_associations = (array) get_post_meta( $post_id, "{$taxonomy}_associated_posts", true );
96+
$existing_associations = array_filter( $existing_associations );
97+
98+
$new_term = wp_insert_term( $title_after, $taxonomy );
99+
100+
if ( is_wp_error( $new_term ) ) {
101+
return;
102+
}
103+
104+
foreach ( $existing_associations as $association ) {
105+
wp_set_object_terms( $association, $new_term['term_id'], $taxonomy );
106+
}
107+
108+
return;
109+
}
110+
90111
// If the post transitioned from published to not published, remove the associated term.
91112
if ( 'publish' !== $status_after && $term_before ) {
92113
$associated_posts = get_objects_in_term( $term_before->term_id, $taxonomy );

tests/test-term-sync.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,44 @@ public function test_post_trash_to_publish_creates_term_and_restores_relationshi
539539
$this->assertEquals( array( $term->slug ), $associated_terms, 'Existing shadow term relationships should be restored when its connected post is moved from trash to publish.' );
540540
}
541541

542+
/**
543+
* A published post without a shadow term should have one created on update.
544+
*/
545+
public function test_post_publish_to_publish_creates_missing_term(): void {
546+
$post = $this->factory()->post->create(
547+
array(
548+
'post_type' => 'example',
549+
'post_title' => 'Hazelnut',
550+
'post_status' => 'publish',
551+
)
552+
);
553+
554+
if ( is_wp_error( $post ) ) {
555+
$this->fail( 'Failed to create post.' );
556+
}
557+
558+
$post = get_post( $post );
559+
$term = get_term_by( 'slug', 'hazelnut', 'example_connect', 'OBJECT' );
560+
561+
if ( ! $term ) {
562+
$this->fail( 'Expected term not available.' );
563+
}
564+
565+
// Delete the shadow term to simulate the missing term condition.
566+
wp_delete_term( $term->term_id, 'example_connect' );
567+
568+
$term = get_term_by( 'slug', 'hazelnut', 'example_connect', 'OBJECT' );
569+
$this->assertFalse( $term, 'Shadow term should have been deleted.' );
570+
571+
// Update the post without changing its status.
572+
wp_update_post( $post );
573+
574+
$term = get_term_by( 'name', 'Hazelnut', 'example_connect', 'OBJECT' );
575+
$term_name = ! $term ? '' : $term->name;
576+
577+
$this->assertEquals( 'Hazelnut', $term_name, 'A published post without a shadow term should have one created on update.' );
578+
}
579+
542580
/**
543581
* An existing published post that has its title changed should change the
544582
* title of its shadow term.

0 commit comments

Comments
 (0)