【问题标题】:Automatically generate tags based on Post Object (ACF) titles in WordPress根据 WordPress 中的帖子对象 (ACF) 标题自动生成标签
【发布时间】:2021-11-01 00:31:55
【问题描述】:

我正在为我们的项目页面创建自定义帖子类型。我还为我们的员工制作了自定义帖子类型。

通过 ACF,我创建了一个关系字段,您可以在其中将团队成员添加到项目中,并显示在网站上。

根据关系字段中选定的团队成员帖子,我希望为关系字段中加载的每个职位(员工姓名)生成一个标签。

这是我现在被卡住了。

帖子对象中的名称称为teamleden。我尝试将代码添加到我的海关邮件类型文件中,但它不起作用。

<?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;

}

【问题讨论】:

    标签: wordpress advanced-custom-fields custom-post-type


    【解决方案1】:

    这未经测试,但应该能让你走上正轨。

    您需要将其添加到您的function.php

    在我的示例代码中引用您需要更改的内容...

    1. projects post_type 是此代码在保存或更新帖子时触发的帖子类型名称。
    2. employeesprojects 帖子类型中acf 关系字段的名称。
    3. team_members 是您应该在 projects 帖子类型上使用的自定义标签分类名称。

    基本上,这段代码的作用是在使用acf/save_post 操作保存、发布或更新projects 帖子时。它获取此帖子的 acf 关系字段成员数据。如果字段中有成员,则获取所有现有的团队成员标签,并通过 ID => Title(name) 创建一个现有成员标签的简单数组。

    然后它遍历关系字段中的所有成员,并检查成员的标签是否已经存在,如果存在,它将现有的成员标签(int) ID 添加到$team_member_tags_to_set 数组中。如果没有找到现有的成员标签,我们只需将成员标题(名称)作为(string) 添加到$team_member_tags_to_set 数组中。

    完成所有这些后,我们只需使用 wp_set_post_tags() 传递我们的 $team_member_tags_to_set 数组来更新当前 projects 帖子的 team_members 分类术语。

    我还在 wp_set_post_tags() 中将 append 设置为 false,这会删除所有以前的标签并创建一组新标签。如果成员在 acf 关系字段中得到更新,这将有所帮助。

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

    查看下面的代码,并阅读我的 cmets,以便了解发生了什么。

    <?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;
    
    }
    

    【讨论】:

    • 非常感谢,我让它工作了,唯一的是 - 它现在注册到全局标签而不是自定义分类法。但我已经对此感到满意了。
    • 感谢您的帮助,它唯一没有做的就是将其注册到 custom-taxonomy 但它确实注册到了一般分类标签。这对我来说很好 2 :-)
    • 可能值得为团队成员注册一个自定义分类法,因为这些成员标签会混入您的帖子标签中,这会很混乱。可能更好地保持所有成员标签合并,这样搜索数组就不会变大
    • 这就是我遇到障碍的地方。我已经在 team_members 下注册了自定义分类,这与上面的代码相同。但它仍然在一般分类标签中注册everyting。我一直在摆弄不同的名字等,但它没有改变。它使其在一般标签分类中注册。而不是定制的。
    • 我得到它与 wp_set_object_terms 一起工作 感谢您的时间和帮助 ;-) wp_set_post_terms 仅适用于本机帖子,而不适用于自定义帖子类型
    猜你喜欢
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多