Home Forums ACF PRO Save User ID as Term Meta


Save User ID as Term Meta

  • Term Meta question.

    I am using ACF to have a frontend form (using acf_form). This form creates posts in a CPT. The CPT has a custom taxonomy associated with it.

    When a term is created, I want to use add_term_meta() to attach the logged in user’s ID as a meta value to that term.

    I was thinking that could be done using the ‘create_term’ hook, but I can’t find any examples.

    Any suggestions or hints would be GREATLY appreciated.
    Whether it’s the ‘acf/save_post’ hook, the ‘create_term’ hook, or something else… I’m a bit lost.


  • Hi @benheath

    There’s probably a couple places you can jump in with your add_term_meta() code I would guess, but acf/save_post is probably a really good choice. Do you have reservations about using that?

    Sometimes I find it tricky when doing things during the “save” process whether ACF fields are set yet or not… whether to use the Field Key, or whether to use the $POST object… and of course, it all depends on the purpose…

    … but my new favourite action hook is: wp_insert_post

    … maybe give that one a try if you have troubles with acf/save_post … it works for me every single time when coupled with ACF custom fields.

  • The goal is to add the user ID as term meta when a term of a custom Taxonomy is created.

    Since I’m using an ACF field for the frontend user (logged in only) to be able to add terms if needed, I thought the acf/save_post hook might be easiest. I’m just unsure of how to exactly write the code.

    I am good with editing php, if I have an example of something close to what I’m wanting… but I’ve just been coming up empty on this particular need.

    $term_id = Not sure where I'd get this from within a hook during term creation.
    if ($term_id is new) {
      $user_id = get_current_user_id();
      add_term_meta($term_id,$user_id,'term_author_id',true); //term would be unique, and have unique meta (unique author)

    I’m not sure I’m even on the right track with my logic.

    When a post is being created or edited, if a new term has been created, that’s when I want to add the ID of the user that is doing it as term meta… so that I can later show terms made by that author back to them.

    If you could provide an example, or even the exact code you think would do it that would be AWESOME.
    Thank you!

  • Hey @benheath

    It would be great if there was a hook we can tap into each time a new Taxonomy was created. That process is being done with JavaScript, and I don’t know of any hook at that step. But that would present another issue… the User could end up not using the Term in the end… so that would need more thinking anyway.

    With that said… I asked myself these questions..

    I know it would be easy to run add_term_meta() on ‘save’… that’s not an issue.

    What I would like to know is…

    1) How would we know if a term has been newly added by this User?
    2) Can we assume that if ‘term_author_id’ is NOT set, it’s new? Probably.

    So… after a Post is Published/Saved… even within the wp_insert_post hook, we can:

    Iterate through all terms attached to *that* post. Look for ones that don’t have term_author_id set, and assume (safely) that the current User came up with that term.. then we run run add_term_meta() function.

    I don’t have the exact code for you… but do it bit by bit, and use var_dump each step of the way to see if you are getting what you are supposed to.

    Here’s a good reference:

    Also… there are heaps of functions specific to Terms/Taxonomies for WordPress… get_terms and get_the_terms seem to be good choices here.

    Sorry that I couldn’t be more help, but this should get you moving along further I hope!

  • Ok thank you for some of your logic help on this.

    What I ended up doing is adding code on the single-CPT.php template that does this:

    $client_name = get_the_terms(get_the_ID(),'client-name');
    $post_author_id = get_the_author_meta('ID');
    $term_id = $client_name[0]->term_id;
    if(!get_term_meta($term_id,'term_author_id')) {
    	add_term_meta($term_id, 'term_author_id', $post_author_id, true);

    So, on the post, it get’s the term from the taxonomy (this post type will only ever have ONE term assigned to it from that taxonomy). Then it checks if the term has the term meta already, and if it doesn’t, it assigns the author ID of that post as the term_author_id value. That way the code only runs if it needs to.

    I decided on this method, since when the user creates a post in this post type, they have to also assign an existing term, or create one, and then is redirected to that post after creation. So that single template file will load immediately after the post is created, this code will run, and everything seems to be working as intended.

    My current issue is in the ACF field where the user picks the Taxonomy Term, it is pulling in ALL of the terms still. I have written code to only show terms in other areas:

    $taxonomy = 'client-name';
     $terms = get_terms($taxonomy, array(
          'hide_empty' => false,
          'meta_key' => 'term_author_id',
          'meta_value' => get_current_user_id()

    This works great to only display the terms for the logged in user in other areas… and I thought I saw a WP filter that I could apply this logic to EVERYWHERE the terms would be displayed… but now I can’t find it.

    Any idea on how this could be done in the ACF field at least?

    Perhaps this?

    Or is there a better way?

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Save User ID as Term Meta’ is closed to new replies.