有一个名为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;
}
谢谢