Spyker55
Spyker55

Reputation: 15

Automatically generate tags based on Post Object (ACF) titles in WordPress

I'm creating a custom post type for our projects page. I've also made a custom posts type for our employees.

With ACF I've made a Relationship field which where you can add the team members to a project and it's displayed on the website.

Based in the selected team members posts in the relationship field I would like the generate a tag for each title (Employee name) that is loaded in the relationship field.

This is were I'm stuck now.

The name in the Post Object is called teamleden. I've tried adding code to my customs posts type file but it doesn't work.

<?php

// save post action
add_action('acf/save_post', 'set_employee_tags_on_save_update', 20);

/**
 * @param $post_id int|string
 */
function set_employee_tags_on_save_update($post_id) {

    // get our current post object
    $post = get_post($post_id);

    // if post is object
    if(is_object($post)) {

        // check we are on the projects custom type and post statuses are either publish or draft
        // change 'projects' post type to your post type name which has the relationship field
        if($post->post_type === 'projects' && ($post->post_status === 'publish' || $post->post_status === 'draft')) {

            // get relationship field employees
            // this example uses Post Object as the Return Format and is a multiple value
            // change get field param 'employees' to your relationship field name
            $employees = get_field('employees');

            // team member tags to set empty array
            $team_member_tags_to_set = [];

            // if employees has value or values
            if($employees) {

                // get all of our current team member tags
                // change 'team_members' taxonomy value to your members taxonomy name
                $team_member_tags = get_terms([
                    'taxonomy' => 'team_members',
                    'orderby' => 'name',
                    'order' => 'ASC'
                ]);

                // empty array for existing team member tags
                $existing_team_member_tags = [];

                // if we have existing team member tags
                if(!empty($team_member_tags)) {

                    // foreach team member tags as team member tag
                    foreach($team_member_tags as $team_member_tag) {

                        // add existing team member to our existing team member tags array by tag ID => tag name
                        // this is so we can use this later to check if a team member tag already exists so we dont create duplicates
                        $existing_team_member_tags[$team_member_tag->ID] = $team_member_tag->name;

                    }

                }

                // foreach employees as employee
                foreach($employees as $employee) {

                    // get the title for current employee
                    $title = get_the_title($employee->ID);

                    // search existing team members array and return the tag id via key
                    $existing_team_member_tag_id = array_search($title, $existing_team_member_tags);

                    // if we have an existing team member tag id
                    if($existing_team_member_tag_id) {

                        // add the existing team member tag id as integer to array
                        $team_member_tags_to_set[] = (int)$existing_team_member_tag_id;

                    } else {

                        // else create a new tag for team member by adding title (name) as string to array
                        $team_member_tags_to_set[] = (string)$title;

                    }

                }

            }

            // remove the action
            remove_action('acf/save_post', 'acf_save_post');

            // set post tags for this post id, removing any unused team member tags if relationship field team members are changed
            wp_set_object_terms($post_id, $team_member_tags_to_set, $taxonomy = 'team_members', false);

            // re add the action
            add_action('acf/save_post', 'acf_save_post');

        }

    }

    // finally return
    return;

}

Upvotes: 0

Views: 1271

Answers (1)

joshmoto
joshmoto

Reputation: 5088

This is not tested but should get you on the right track.

You will need to add this in your function.php.

Reference to things you will need to change in my example code...

  1. projects post_type is the post type name which this code fires when post is saved or updated.
  2. employees is the name of the acf relationship field in the projects post type.
  3. team_members is the custom tag taxonomy name which you should have working on your projects post type.

Basically what this code does, is when a projects post is saved, published or updated using the acf/save_post action. It gets the acf relationship field member data for this post. If there are members in the field, it then gets all existing team member tags and creates a simple array of existing member tags by ID => Title(name).

It then loops through all members in the relationship field, and checks if tag already exists for member, if it does it adds the existing member tag (int) ID to the $team_member_tags_to_set array. If no existing member tag is found, we just add the member title (name) as a (string) to the $team_member_tags_to_set array.

After all this is done, we simply use wp_set_post_tags() passing our $team_member_tags_to_set array to update team_members taxonomy terms for the current projects post.

I've also set append to false in wp_set_post_tags() which removes all previous tags and creates a new set of tags. This will help if members get updated in the acf relationship field.

https://developer.wordpress.org/reference/functions/wp_set_post_tags/

See code below, and read my comments so you know whats happening.

<?php

// save post action
add_action('acf/save_post', 'set_employee_tags_on_save_update', 20);

/**
 * @param $post_id int|string
 */
function set_employee_tags_on_save_update($post_id) {

    // get our current post object
    $post = get_post($post_id);

    // if post is object
    if(is_object($post)) {

        // check we are on the projects custom type and post statuses are either publish or draft
        // change 'projects' post type to your post type name which has the relationship field
        if($post->post_type === 'projects' && ($post->post_status === 'publish' || $post->post_status === 'draft')) {

            // get relationship field employees
            // this example uses Post Object as the Return Format and is a multiple value
            // change get field param 'employees' to your relationship field name
            $employees = get_field('employees');

            // team member tags to set empty array
            $team_member_tags_to_set = [];

            // if employees has value or values
            if($employees) {

                // get all of our current team member tags
                // change 'team_members' taxonomy value to your members taxonomy name
                $team_member_tags = get_terms([
                    'taxonomy' => 'team_members',
                    'orderby' => 'name',
                    'order' => 'ASC'
                ]);

                // empty array for existing team member tags
                $existing_team_member_tags = [];

                // if we have existing team member tags
                if(!empty($team_member_tags)) {

                    // foreach team member tags as team member tag
                    foreach($team_member_tags as $team_member_tag) {

                        // add existing team member to our existing team member tags array by tag ID => tag name
                        // this is so we can use this later to check if a team member tag already exists so we dont create duplicates
                        $existing_team_member_tags[$team_member_tag->ID] = $team_member_tag->name;

                    }

                }

                // foreach employees as employee
                foreach($employees as $employee) {

                    // get the title for current employee
                    $title = get_the_title($employee->ID);

                    // search existing team members array and return the tag id via key
                    $existing_team_member_tag_id = array_search($title, $existing_team_member_tags);

                    // if we have an existing team member tag id
                    if($existing_team_member_tag_id) {

                        // add the existing team member tag id as integer to array
                        $team_member_tags_to_set[] = (int)$existing_team_member_tag_id;

                    } else {

                        // else create a new tag for team member by adding title (name) as string to array
                        $team_member_tags_to_set[] = (string)$title;

                    }

                }

            }

            // remove the action
            remove_action('acf/save_post', 'acf_save_post');

            // set post tags for this post id, removing any unused team member tags if relationship field team members are changed
            wp_set_post_tags($post_id, $team_member_tags_to_set, false);

            // re add the action
            add_action('acf/save_post', 'acf_save_post');

        }

    }

    // finally return
    return;

}

Upvotes: 1

Related Questions