Title: Nested Loop Issues
Last modified: August 21, 2016

---

# Nested Loop Issues

 *  Resolved [lezlyprime](https://wordpress.org/support/users/lezlyprime/)
 * (@lezlyprime)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/nested-loop-issues-1/)
 * I’m trying to write a loop that gets all users & if the user has posts, list 
   the post titles. I’d also like to get a count of users.
 * Code:
 *     ```
       <?php
       $args = array(
       	'orderby' => display_name
       );
       // The Query
       $user_query = new WP_User_Query( $args );
       $x = $total-users; //returns 0
       echo '<p>Total users: ' . $x . '</p>';
       // Loop
       if ( ! empty( $user_query->results ) ) {
       	echo '<p>Number of users: ' . count($user_query) . '</p>'; //returns 1
       		foreach ( $user_query->results as $user ) {
       			echo '<p>User name: ' . $user->first_name . '</p>';
       			$args4 = array (
       				'author_name' => $user->display_name,
       				'orderby' => 'title'
       			);
       			//The Query
       			$the_query4 = new WP_Query( $args4 );
       			//The Loop
       			if ( $the_query4->have_posts() ) {
       				echo '<ul>';
       				while ( $the_query4->have_posts() ) {
       					$the_query4->the_post();
       					echo '<li>' . get_the_title() . '</li>';
       			}
       			echo '</ul><br />';
       			} else {
       				echo '<p>No posts were found for ' . $user->display_name . '</p>';
       			}
       		}
       } else {
       	echo '<p>No users were found.</p>';
       }
       ?>
       ```
   
 * Output:
 * Total users: 0
 * Number of users: 1
 * User name: Booyah
 * Booyah’s post
 * User name: Clea
 * String Theory
 * User name: Lezly
 * You Are Here
    Using Time Travel to Manage Your Time Politics 101 HAL Candidates?
   What candidates?
 * User name: Zoe
 * No posts were found for Zoe Travertine
 * ===========
 * Problem is, Zoe has five published posts, but my code isn’t getting those. What
   am I missing?

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/nested-loop-issues-1/#post-4985984)
 * `total_users` is a property of the user_query object. Reference it like this:`
   $x = $user_query->total-users;`
 * Similarly, the number of users is a count of the results array: `echo '<p>Number
   of users: ' . count($user_query->results) . '</p>';`
 * Finally, the author_name parameter should be the user_nicename instead of display_name:
 *     ```
       'author_name' => $user->user_nicename,
       ```
   
 *  Thread Starter [lezlyprime](https://wordpress.org/support/users/lezlyprime/)
 * (@lezlyprime)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/nested-loop-issues-1/#post-4986015)
 * That fixed it right up! Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Nested Loop Issues’ is closed to new replies.

## Tags

 * [nested loops](https://wordpress.org/support/topic-tag/nested-loops/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [lezlyprime](https://wordpress.org/support/users/lezlyprime/)
 * Last activity: [11 years, 11 months ago](https://wordpress.org/support/topic/nested-loop-issues-1/#post-4986015)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
