0

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.

  • 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 Dec 3 '18 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 Dec 3 '18 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 Dec 3 '18 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 Dec 4 '18 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 Dec 4 '18 at 2:49
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).

  • :( it's not working. I use post tag and bbpress topic tag for example, they are not sync – Bryan Dec 21 '18 at 9:48
  • Can you post on Pastebin the code you attempted? – Sally CJ Dec 21 '18 at 23:59
  • pastebin.com/6xF7pYMB – Bryan Dec 23 '18 at 6:28
  • Make sure to use the proper taxonomy slug - in bbPress, the taxonomy is topic-tag and not topic.. check /wp-admin/edit-tags.php?taxonomy=topic-tag&post_type=topic on your site. So try $taxonomies = ['topic-tag', 'post_tag'];. – Sally CJ Dec 23 '18 at 7:06
  • Did the code work for you?? If not, what was your solution? If yes, please mark the answer as complete ("accept" it) so that people know the question has an accepted answer. Thank you. – Sally CJ Mar 15 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.