Support

Account

Home Forums General Issues Showing post object field values within a wp_user_query loop Reply To: Showing post object field values within a wp_user_query loop

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