Support

Account

Home Forums ACF PRO Save User ID as Term Meta Reply To: Save User ID as Term Meta

  • 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?
    https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

    Or is there a better way?