There is a Wordpress function called wp_remove_object_terms that unfortunatly doesn't seem to work.
I am trying to achieve the same ends using the following
$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 );
}
This doesn't work, not sure where I am going wrong. Here is the code in the context of the function it sits in
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;
}
Thanks
There is a Wordpress function called wp_remove_object_terms that unfortunatly doesn't seem to work.
I am trying to achieve the same ends using the following
$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 );
}
This doesn't work, not sure where I am going wrong. Here is the code in the context of the function it sits in
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;
}
Thanks
Share Improve this question asked Mar 13, 2014 at 19:37 phantomdentistphantomdentist 1012 bronze badges2 Answers
Reset to default 1Your issue is last argument in wp_set_object_terms()
call. You are setting $append
to true
, so it essentially checks if input is already present and does nothing else.
You want it set to false
(which is also default) so that terms are forced to be same as input and difference (what you removed) is deleted from post.
I'm very late to the party but the error is that you are searching a multidimensional array. You have to specify for the key you are searching for
$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 );
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745414870a4626701.html
评论列表(0条)