Support

Account

Home Forums ACF PRO User taxonomies

Solving

User taxonomies

  • There’s a great plugin that recently came back to life that allows taxonomies to be attached to a user.

    https://wordpress.org/support/plugin/lh-user-taxonomies

    However, even though the taxonomy appears in the ACF dropdown for the taxonomy field, it does not actually update the terms on save like it does for posts/pages/etc.

    Wondering if this is an ACF thing that can fix this or a LH User Taxonomies issue?

  • I haven’t played with it yet, but recently discovered this plugin as well. I’m researching for a project that will require front end ‘profile’ management with custom fields. I was considering syncing each user with 1 custom post.. but that feels so difficult. I’d much rather just have a custom taxonomy for users. Gonna have a play with this one soon.

  • I’m looking at this same thing now (2 months later) and I’ve found that Advanced Custom Fields isn’t attaching a user to a taxonomy properly.

    Instead of properly adding an entry to the wp_term_relationships table they’re just adding an entry to the wp_user_meta table.

  • Interesting. I hadn’t dug into the code, but this seems like it’s the problem. Hopefully someone at ACF sees this and can do a bug fix!

  • Also, note this thread, which seems to imply there might be a core fix for this in the near future.
    https://core.trac.wordpress.org/ticket/31383

  • Since there is not official support as yet for user taxonomies, I’m not sure that this is something the developer will want to add to ACF. I will flag this topic for his attention.

  • I’ve successfully registered a custom taxonomy and assigned it to the ‘user’ object. It actually all works fine. The only issue is there is no default UI for managing the taxonomy. I have CMB2 (ACF may work too) to choose the taxonomy term(s) for the user.

    A tip on how to manage the taxonomy:

    Also attach the custom taxonomy to another post type (like posts), then you can add the terms, descriptions, etc via the Posts/taxonomy UI and they will show up (via ACF/CMB2) in the user profile.

    It’s working well for me this way right now.

  • Hi guys,

    This is really exiting stuff!

    Be sure to post any news regarding user taxonomies here and I’ll do my best to add in the compatibility into ACF

  • Hi all,

    I would really appreaciate some advice on the best way to resolve a problem with the Taxonomy Field Type when applied to Users.

    I have categories and custom taxonomies set up as taxonomy fields with the field group set to appear on user profiles. The taxonomy field type is checkbox with allow multiple select, Load and Save options set to Yes.

    After editing a user profile ‘John Doe’ and selecting terms from the taxonomies the term_ids are correctly stored as a serialized array in the wp_usermeta table. However the entries that are created in the wp_term_relationships table have object_id = 0, rather than the user_id for ‘John Doe’. When I edit another user profile ‘Fred Bloggs’ the terms applied to ‘John Doe’ are loading in the taxonomy field. Applying new terms to ‘Fred’ shows that the terms simply accumulate.

    I can see that ACF only sets post_ids (not user_id) as the object_id when it performs wp_set_object_terms() acf/fields/taxonomy.php. I assume that this is to avoid a potential data conflict issue where a post_id and user_id are the same.

    To help avoid the conflict I have emptied my database set the autoincrement value on the wp_users table to 1000000 to reserve ids 1-1000000 for posts and 1000000+ for users.

    Can anyone suggest some code that would enable me to hook into the ACF save terms process and allow user_ids perhaps using

    wp_set_object_terms( $user_id, $term_ids, $taxonomy, false );

    Another alternative would be to create 1 custom post for each user which holds their profile data but that feels over complicated and unnecessary.

  • Hi guys

    Great news!
    I’ve just spent the morning improving some core functionality surrounding the $post_id, and have been able to add in compatibility to save/load terms to a user!

    using the tutorial you linked me to, you can now register a user taxonomy, and simply use an ACF taxonomy field to add/edit/connect taxonomies to users!

    I’ll be including this in the next version of ACF PRO.
    If you wish to test out the new functionality, please login to your account online, click the ‘view all versions’ link next to your license and download the ACF PRO 5.4.0 beta3 version!

    Thanks
    Elliot

  • @elliot

    Please could I back up a bit to understand this and what it means for me… ?

    1. So, previously, with ACF, I could add a Taxonomy field to an Edit User page? (I have successfully done this – both standard “Categories” taxonomy and multiple custom taxonomies).
    2. But, although ACF could successfully show the Taxonomy dropdowns/checkboxes, it could not save to them, so as as to link the User to the Taxonomy?
      Now, you have made a change to support this?

    But…

    • Does this mean I should still be using a separate plugin, ie. LH User Taxonomies, in conjunction with ACF?
    • Or can I now do this just with ACF, as long as I set up a Taxonomy ready for ACF?

    In my case, I want to categorise my Users using some custom taxonomies. I have already set up two custom taxonomies for this (“Industries” and “Companies”).

    So, if I now use ACF to add them as fields to my Edit User pages, will they successfully save as a result of your upgrade? Or do I still need to be employing LH Taxonomies?

    One caveat/FYI: I had previously registered my custom taxonomies using manual code in functions.php, but currently they are set up using WCK, Worpress Creation Kit.
    Now, is it relevant that WCK’s Taxonomy Creator includes an option to “Attach” my taxonomies to: “post”, “page” and “mycustomposttype” (in other words, it only shows options for Posts, not Users)?

    Will ACF be able to successfully connect with these taxonomies, I wonder? Will it override the WCK setting?

  • I have just saved checked some taxonomy items in several of my taxonomy fields displayed by ACF on an Edit User page, and clicked Update User.

    The items come back with checkboxes still ticked on the page reload.
    So, in what way does/did “User taxonomies” not work with ACF?

    (FYI, I am on ACF Pro 5.3.9.2)

  • Hi @iamrobertandrews

    The issue was that ACF was not able to connect a term object to a user object via the wp_term_relatinoships table.

    Previously, only the meta data was stored (array of term ID’s), but now you can use the ‘save and load’ settings to connect them via this table.

    This allows for tax queries

  • This looks good but how would you query the users now ? with get_users ? or with new WP_User_Query ? And like this ?

    // WP_User_Query arguments
    $args = array (
        'who'           => 'all',
        'tax_query' => array(
        array('taxonomy' => 'mytaxonomy', 
        'field' => 'id', 
        'terms' => $term_id ) ) 
    );
    
    // The User Query
    $user_query = new WP_User_Query( $args );
  • I don’t think WP_User_Query supports taxonomies yet. I usually have to use https://codex.wordpress.org/Function_Reference/get_objects_in_term

  • Ok, thank you. I was referring to Elliot’s last post: he says that now ACF stores and sync correctly taxonomy data for users. So that allows tax queries for users. But as you point, Wp_User_Query doesn’t support taxonomies yet and neither get_users… So I’m wondering if there is another way to query users ?
    I’m currently using get_queried_object to get the term id (on a custom taxonomy template page) and get_objects_in_term to get the users. But among the returned objects there are some posts, page and menu-items ! I could filter them out based on their post type but it would be much cleaner to query the users directly. This might be due to some remnants of old relationships present in the database because currently this custom taxonomy is only attached to users (through the use of the lh taxonomy plugin).

  • I’ve never seen that. You may have had some shared terms via an older version of WP, and when the term split happened in WP something broke. I use custom taxonomies on users all the time, and have only ever returned users when using get_objects_in_term()

  • Hey guys,

    I’m having the same problem as @iamrobertandrews. I have a User register form in which I have some taxonomies, and I use those taxonomy terms checked by user for filtering users in a page.

    I have a ACF User form, and on this form I have these taxonomy related fields. Then, I’m using LH User Taxonomy plugin for having a User Taxonomy UI and also to have this working.

    All things was working fine until 03/03/2017 (user was registering, then taxonomy field terms was populating User Taxonomies correctly, automatically). But, for some reason it stopped to work on 03/03/2017 (date of last user register that recorded taxonomies correctly).

    The weird thing, I did the @iamrobertandrews experiment, which means, getting a registered user that comes with blank taxonomy feilds, then I checked some terms manually on User Edit page, and then saved. For some reason, it worked! Buuuuuut, I need this working from the form, because I can’t know what item the user filled if it comes blank…

    So, I was wondering if you guys have any idea about why it was working and from 03/03 it isn’t anymore? Where should I look for a bug (something that I changed and make it stop to work), or if you have any suggestion for getting it working again.

    Thanks in advance.

    Cheers.

    ACF version 5.5.11

    Form: http://www.cieb.net.br/cadastro-pessoa-fisica/

    Form WP Page:

    <?php
    acf_form_head();
    get_header();
    ?>
    
    	<!-- #Content -->
    	<div id="Content">
    		<div class="content_wrapper clearfix">
    	
    			<!-- .sections_group -->
    			<div class="sections_group">
    
                	<div class="container">
                    	<div class="row form-cadastro-especialista">
    				<?php if ( have_posts() ) : ?>
                        <div class="col-xs-12"> 
                                    <?php while ( have_posts() ) : the_post(); ?>
    
                                         </div>
     
                                          <form action="<?php echo site_url('wp-login.php?action=register&role=especialista', 'login_post') ?>" method="post">
                                          	<div class="acf-field">
                                                <div class="acf-label">
                                                  <label><span class="numero-questao">1</span>Nome de Usuário (login)</label>
                                                </div>
                                                <div class="acf-input">
                                                <div class="acf-input-wrap">
                                                  <input type="text" name="user_login" value="Nome de Usuário" id="user_login" class="input" />
                                                  </div>
                                                  <p class="description">Seu nome de usuário (login) poderá ser utilizado futuramente para que você utilize outros serviços da Rede IEB.</p>
                                                </div>
                                              </div>
                                              <div class="acf-field">
                                                <div class="acf-label">
                                                	<label><span class="numero-questao">2</span>Seu E-mail</label>
                                                </div>
                                              <div class="acf-input">
                                                <div class="acf-input-wrap">
                                            
                                                <input type="text" name="user_email" value="E-Mail" id="user_email" class="input"  />
                                                </div>
                                                <p class="description">Você receberá em seu e-mail um link para cadastrar sua senha.</p>
                                              </div>
                                             </div>
                                              <?php do_action('register_form'); ?>
                                              <input type="submit" value="Cadastrar" id="register" />
                                              <input type="hidden" name="redirect_to" value="/solicitacao-cadastro-pessoa-fisica"/>
                                          </form>
                                            
                                    <?php endwhile; ?>
                                
                            <?php else : ?>
                                <p><?php _e( 'Desculpe, não há conteúdo disponível para esta página ainda. Volte em breve.', 'cieb' ); ?></p>
                            <?php endif; ?>
                            </div>
    					</div><!--row-->
                        
    				</div><!--container-->
                </div><!--sections_group-->
    	
    		</div><!--content_wrapper-->
    	</div><!--#Content-->
    	
    	<?php get_footer();?>
  • Hi all,

    I installed LH User Taxonomies and created a taxonomy with the suggested snippet in the plugin’s description.

    I then created an ACF taxonomy field for user objects. I can save terms via that field on the user edit screen and they save perfectly, but they aren’t able to connect a term object to a user object via the wp_term_relatinoships table – as @elliot mentioned he implemented in ACF 5.4.0, above.

    Has any one had any success with this? Is there a good workaround for hooking into the ACF save process like @rowanpurdy suggested?


    @arcanepsyche
    , @jivedig, @lbonner – you guys have any success? Because of Elliot’s updates on this thread I was expecting that part of the process to work natively with ACF.

  • IF anyone is interested, I got it working.

    For some reason the field had a random name instead of the one I gave it in the ACF admin screen. I left a little snippet in so you can easily find the field name through debugging. There is probably a better way to do this but I’m not intimately familiar with all things ACF.

    Also note that my custom taxonomy is “expert” so change that out for yours.

    add_action('edit_user_profile_update', 'wda_connect_user_terms');
    add_action('personal_options_update', 'wda_connect_user_terms');
    
    function wda_connect_user_terms($user_id) {
    
      $terms = $_POST["acf"]['field_59eeaa8ebb291'];
    
    // comment in this snippet to find field name
    /* $fields = $_POST;
     * echo '<pre>';
     * var_dump($fields);
     * echo '</pre>';
    */
    
      if ($terms) {
       
      // An array of IDs of categories we want this post to have.
      $cat_ids = $terms;
    
      /*
       * If this was coming from the database or another source, we would need to make sure
       * these were integers:
      */
    
      $cat_ids = array_map( 'intval', $cat_ids );
      $cat_ids = array_unique( $cat_ids );
    
      $term_taxonomy_ids = wp_set_object_terms( $user_id, $cat_ids, 'expert' );
    
      if ( is_wp_error( $term_taxonomy_ids ) ) {
        // put debug code here
      } else {
        // put debug code here
      }
    
      }
    
    }
Viewing 22 posts - 1 through 22 (of 22 total)

The topic ‘User taxonomies’ is closed to new replies.