$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Sync All Post Type Tag|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Sync All Post Type Tag

matteradmin8PV0评论

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).

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far