Removing a taxonomy term

时间:2014-03-13 作者:phantomdentist

有一个名为wp\\u remove\\u object\\u terms的Wordpress函数,不幸的是,它似乎不起作用。

我试图通过以下方法达到同样的目的

    $post_terms = wp_get_object_terms( $post_id, $taxonomy );



                if( $post_terms ){
                    if(($key = array_search($term_id, $post_terms)) !== false) {
                        unset($post_terms[$key]);
                    }
                    wp_set_object_terms( $post_id, $post_terms, $taxonomy, true );
                }
这行不通,我不知道我哪里出了问题。下面是它所在的函数上下文中的代码

add_action(\'admin_init\', \'update_custom_tour_groups\');

    function update_custom_tour_groups(){

        $taxonomy = \'custom\';

        $terms = get_terms( $taxonomy, array( "hide_empty" => 0 ) );

        foreach( $terms as $term ):

            //Get the term id of the current term
            $term_id = $term->term_id;
            //Create the acf string to get the acf field on the current term page eg custom_awesome-tours
            $acf_value = $taxonomy.\'_\'.$term_id;

            //wp_set_post_terms, used later, needs to term id as a number (int)
            $term_id_int = (int)$term_id;

            //Loop through all tours in the custom taxonomy and remove the term we are currently looping through
            $args = array(
                \'post_type\' => \'tours\',
                \'posts_per_page\' => -1
            );

            $the_query = new WP_Query($args);       
            if( $the_query->have_posts() ): 

                while ( $the_query->have_posts() ): 

                    $the_query->the_post();

                    $post_id = $post->ID;

                    //$post_id_int = (int)$post_id;

                    //Remove the current term from this post. Basically this remove all posts from the current term
                    //wp_remove_object_terms( $post_id_int, $term_id, $taxonomy );
                    //wp_delete_object_term_relationships( $post_id, $taxonomy );
                    $post_terms = wp_get_object_terms( $post_id, $taxonomy );

                    /*
                    echo 
                    \'
                    <script>
                    alert("\'.$post_terms.\'");
                    </script>
                    \';
                    */

                    if( $post_terms ){
                        if(($key = array_search($term_id, $post_terms)) !== false) {
                            unset($post_terms[$key]);
                        }
                        wp_set_object_terms( $post_id, $post_terms, $taxonomy, true );
                    }

                endwhile; 
            endif;

            //Loop through each tour that has been selected on this term page
            $post_objects = get_field(\'tours_in_group\',$acf_value);


                if( $post_objects ): 

                    foreach( $post_objects as $post): // variable must be called $post (IMPORTANT)

                        $post_id = $post->ID;

                        $term_id_array = array( $term_id_int ); //Specially formatted to work with wp_set_post_terms

                        //Add the current term to the selected post
                        wp_set_post_terms( $post_id, $term_id_array, $taxonomy, true );

                    endforeach; 

                endif;

        endforeach; 
    }           
谢谢

2 个回复
SO网友:Rarst

你的问题是wp_set_object_terms() 呼叫您正在设置$appendtrue, 因此,它本质上检查输入是否已经存在,而不做其他任何事情。

您希望将其设置为false (这也是默认设置),因此术语将被迫与输入内容相同,而差异(您删除的内容)将从帖子中删除。

SO网友:SteinB

我来晚了,但错误是您正在搜索多维数组。必须指定要搜索的密钥

$post_terms = wp_get_object_terms( $post_id, $taxonomy ); if( $post_terms ){ if(($key = array_search($term_id, array_column($post_terms, \'term_id\'))) !== false) { unset($post_terms[$key]); } wp_set_object_terms( $post_id, $post_terms, $taxonomy, false ); }

结束

相关推荐

每当我安装/更新插件时,都会收到“Warning:CALL_USER_FUNC_ARRAY()”错误

这是在我将wordpress安装从共享主机移到vps后开始的。完全错误如下:警告:call\\u user\\u func\\u array()要求参数1为有效回调,数组必须在/home/pcsteps/public\\u html/wp includes/plugin中正好有两个成员。php在线199我只有在安装或更新插件时才能得到它。它似乎不会以任何方式影响插件的功能。插件上提到的行。php如下所示:195 do { 196 foreach( (array) current($