My code is simple:
$group_id = get_the_ID();
if ( get_post_meta ($group_id, '_um_groups_shells', true) == 'true'){
wp_set_object_terms( $group_id, array( 'full-shells' ), 'um_group_categories', true );
} elseif ( get_post_meta ($group_id, '_um_groups_shells', true) == 'false'){
wp_remove_object_terms( $group_id, array( 'full-shells' ), 'um_group_categories', true );
}
I have checked that the conditionals are working correctly by using die("Group:". $group_id);
What I discovered is that in the example die("Group:". $group_id);
the $group_id
returns 51478
. But if I use just die($group_id);
then it is empty.
Based on that, I'm assuming that
wp_set_object_terms( $group_id, array( 'full-shells' ), 'um_group_categories', true );
doesn't work because $group_id
must be empty.
If I run it like this:
$group_id = 51478;
wp_set_object_terms( $group_id, array( 'full-shells' ), 'um_group_categories', true );
then it works fine.
Update I tried wpdevloper_j's suggestion of using global $post; $post->ID;
global $post; $post->ID;
instead. But the same problem occurred. I can echo $group_id
and print_r($group_id)
and get the correct id, but when trying to execute with wp_set_object_terms
it seems to be empty.
What is causing $group_id
to be empty?
When I do var_dump ($group_id)
, I receive int(51478)
Updated Solution The problem was with a subsequent line of code:
wp_set_object_terms( $group_id, $formdata[ 'categories' ], 'um_group_categories', false );
The false
parameter was undoing the previous. So it was executing and then updating again after the successful execution.
That was so frustrating. :(
die() — Equivalent to exit
exit ( string $status = ? ) : void
exit ( int $status ) : void
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.