Home > Enterprise >  How to get tag_ID in WordPress functions.php and use it in function?
How to get tag_ID in WordPress functions.php and use it in function?

Time:09-21

I'have some problem with my WordPress functions.php script.

When I go to edit Category of Post, I have URL:

/wp-admin/term.php?taxonomy=category&tag_ID=3&post_type=post&wp_http_referer=/wp-admin/edit-tags.php?taxonomy=category

I need a function to update the field in category with ID from URL.

function overwrite_ratings_category(){
    update_field('field_630dbc7cf3fdd', '432', 'category_'.$_GET['tag_ID']);
}

add_action('edit_term','overwrite_ratings_category');

When I use number (e.g. 3) instead $_GET['tag_ID'] function works fine. But it's not working with $_GET['tag_ID'].

Is there any other way to get the ID of the currently edited category?

CodePudding user response:

$tag_id = get_queried_object()->term_id;

CodePudding user response:

Since you're using edit_term action, it gives your term id as the first param, you can use this param and do your job like the below code:

function overwrite_ratings_category( $term_id ) {
    update_field( 'field_630dbc7cf3fdd', '432', 'category_' . $term_id );
}
add_action( 'edit_term', 'overwrite_ratings_category', 10, 1 );
  • Related