Support

Account

Forum Replies Created

  • 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.

  • Jonathan,

    Wow I must have had a long day yesterday and didn’t even spot that you had already answered my question. Your second point was key and alludes to my failure to understand the key difference between the_title() and get_the_title(). Thanks for taking the time to help me out.

    For the benefit of others:

    • the_title() echos out the title of the current post to the frontend.
    • get_the_title() however returns it as a variable, but does not echo it out
  • Hi Jonathan,

    Thank you for steering me in the right direction and apologies for the poor formatting of the second reply.

    Your first point has corrected my poor syntax but now the data is not showing. I assume this related to your second point.

    What code do you recommend for showing the title of the post object organisation? The user meta field user_organisation stores the ID of the post object organisation and I can’t figure out how to get the post title.

    Many thanks
    Rowan

  • Quick update – the code above is actually outputting the data for the title and link for the organisation that the user is connected to (from the the_title() and the_permalink() calls) however the data appears before the

      tag and wp_user_loop where it is called.

      I changed the code as follows by adding <span> and tags around the_title() and the_permalink() calls but the data remains output before the

        tag and wp_user_loop where it is called yet the new tags <span></span> are shown within the ul minus the data.

        
        <?php 
        $args = array(
          'posts_per_page' => -1,
          'cat' => $queryinfo->term_id	
           );
          $user_query = new WP_User_Query( $args );
          $users = $user_query->get_results();
          if (!empty($users)) {
        	  $people .= '<ul>';
        		  foreach ($users as $user) { // loop through each user
        			  $user_info = get_userdata($user->ID);  // get all the user's data
        			  $people .= '<li><a href="#"><span class="title">' . $user_info->first_name . ' ' . $user_info->last_name . '</span></a>';
        			  if( !empty($user_info->description) ) {
        				  $people .= '<span>' . $user_info->description . '</span>';
        			  }
        			  if( get_field('user_jobtitle', 'user_' .$user_info->ID) ) {
        				  $people .= '<span>' . get_field('user_jobtitle', 'user_' .$user_info->ID) . '</span>';
        			  }
        			  $posts = get_field('user_organisation', 'user_' .$user_info->ID);
        			  // Note that field 'user_organisation' is connected to the post object 'organisation' which is a custom post type
        				if( $posts ) {
        					foreach( $posts as $post) { // IMPORTANT variable must be called $post
        					$people .= '<span><a href="' . the_permalink() . '">' . the_title() . '</a></span>';
        					}
        				wp_reset_postdata();  // IMPORTANT - reset the $post object so the rest of the page works correctly 
        				}
        				$people .= '</li>';
        		   } // end for each loop
        	$people .= '</ul>';
          } // end initial if statement
          else{
        	$people .= '<p>Sorry, we don\'t currently have any people in this category.</p>';
            }
          echo $people;
        ?>
        

        Much head scratching at my end!

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