Sync All Post Type Tag

admin2025-06-05  2

I have a few post types in my website. Is it possible that all post type using the same post tag?

For example:
PostType A have a "Apple" tag.
PostType B, PostType C, PostType D use PostType A "Apple" tag

If this is not possible,I have another question: it possible to sync all post types' tag?

For example: I create a new "ABC" tag in PostTypeA and PostTypeB will automatically has "ABC" tag.

I have a few post types in my website. Is it possible that all post type using the same post tag?

For example:
PostType A have a "Apple" tag.
PostType B, PostType C, PostType D use PostType A "Apple" tag

If this is not possible,I have another question: it possible to sync all post types' tag?

For example: I create a new "ABC" tag in PostTypeA and PostTypeB will automatically has "ABC" tag.

Share Improve this question asked Dec 3, 2018 at 9:15 BryanBryan 55 bronze badges 11
  • When you register the post types, you can add post_tag to the taxonomies parameter, and then those post types would have support for the standard/built-in post tag taxonomy. Then you could edit your posts and assign the same tag to the posts. But I'm not sure if that's what you're looking for? – Sally CJ Commented Dec 3, 2018 at 10:05
  • Thanks, Sally. But it's not. I used about 3 plugins that are created using post type function. All 3 post type have tag taxonomies. For example, if I want to create "Apple" tag for them, I have to manually created 3 times "Apple" in those 3 post type. I want something to auto sync their tag taxonomies, but I have no idea what to do. – Bryan Commented Dec 3, 2018 at 14:29
  • Your post types were added by plugins you created, or someone else? You don't want to sync terms across multiple taxonomies, you want to register a single taxonomy that all 3 will share. – Milo Commented Dec 3, 2018 at 15:52
  • Sorry that my English isn't very good, I will try to explain well. 3 post types were created by 3 different plugins. They all have "Tag" taxonomies. I want to sync them all. If I create a "Apple" tag in PostTypeA, I hope PostTypeB & PostTypeC well also sync that tag which means B & C will auto create one "Apple" tag – Bryan Commented Dec 4, 2018 at 2:32
  • @Bryan, so when for example you edit a post of the post type B, where you add "Tag 1" to the post, do you want all posts of the other post types (e.g. C and D) to also be assigned the same tag "Tag 1"? Which means that all posts of those post types (B, C, and D) will have the exact same tags? – Sally CJ Commented Dec 4, 2018 at 2:49
 |  Show 6 more comments

1 Answer 1

Reset to default 0

If you want to auto-create the same term in another taxonomy, the following code should do it:

You'll add the code to the theme functions file (i.e. functions.php), and make sure to change the $taxonomies which is the list of the applicable taxonomy slugs — the taxonomies that you wish to "sync". Both the $taxonomies should have the exact same values.

But note that this code is intended for terms that do not have parents; or rather, non-hierarchical taxonomies.

$taxonomies = ['my_tax', 'post_tag'];
foreach ( $taxonomies as $taxonomy ) {
    add_action( 'created_' . $taxonomy, 'auto_create_term' );
}

function auto_create_term( $term_id ) {
    $term = get_term( $term_id );
    if ( ! $term || is_wp_error( $term ) ) {
        return false;
    }

    $taxonomies = ['my_tax', 'post_tag'];
    foreach ( $taxonomies as $taxonomy ) {
        if ( $taxonomy !== $term->taxonomy ) {
            remove_action( 'created_' . $taxonomy, __FUNCTION__ );

            wp_insert_term( $term->name, $taxonomy, array(
                'description' => $term->description,
                'alias_of'    => $term->slug,
            ) );

            add_action( 'created_' . $taxonomy, __FUNCTION__ );
        }
    }
}

What the code is doing: We're using the created_{taxonomy} action to auto-clone a term; but we temporarily remove the hook while we're cloning the term — and then add the hook back after the term is cloned (created in the target taxonomy).

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749116591a316503.html

最新回复(0)