[php] Auto-save new tax - voxel
Viewer
- <?php
- function sync_familie_with_taxonomy($post_id, $post, $update) {
- sync_posts_with_taxonomy($post, 'familie', 'familiennamen');
- }
- add_action('save_post_familie', 'sync_familie_with_taxonomy', 10, 3);
- function sync_lageplaene_with_taxonomy($post_id, $post, $update) {
- sync_posts_with_taxonomy($post, 'lageplaene', 'abteilungen');
- }
- add_action('save_post_lageplaene', 'sync_lageplaene_with_taxonomy', 10, 3);
- // Function to sync entries in post type gemeinden with custom taxonomy gemeinde
- function sync_gemeinden_with_taxonomy($post_id, $post, $update) {
- sync_posts_with_taxonomy($post, 'gemeinden', 'gemeinde');
- }
- add_action('save_post_gemeinden', 'sync_gemeinden_with_taxonomy', 10, 3);
- function sync_posts_with_taxonomy($post, $post_type, $taxonomy) {
- // If this is an auto-save, our form has not been submitted, so we don't want to do anything.
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- return;
- // Don't run on WP's post save, but only when the custom post type matches.
- if ($post->post_type != $post_type)
- return;
- // Get all posts from the post type.
- $args = array(
- 'post_type' => $post_type,
- 'post_status' => 'publish',
- 'posts_per_page' => -1,
- 'fields' => 'ids'
- );
- $posts = get_posts($args);
- // For each post, check if its title exists as a term in the taxonomy.
- foreach($posts as $post_id) {
- $post_title = get_the_title($post_id);
- $term = term_exists($post_title, $taxonomy);
- if (!$term) {
- wp_insert_term($post_title, $taxonomy);
- }
- }
- // Check for the "automatisch-gespeicherter-entwurf" term and delete it if it exists.
- $draft_term = term_exists('automatisch-gespeicherter-entwurf', $taxonomy);
- if ($draft_term) {
- wp_delete_term($draft_term['term_id'], $taxonomy);
- }
- }
Editor
You can edit this paste and save as new: