I have a foreach loop inside the loop that is getting info from a separate table. The table has a column that has an id that reflects the id of the post type so it should always be unique.

When I look at the results, the created variable ($fullname) is displayed multiple times in rows with posts that do not share the same id as the $fullname event id.

Here is the code with all of the looping action

        // the loop
        if (have_posts()) : 
                $output.='<table class="contentTable" border="0" width="100%">';
                $output.='<tr class="one-sixth">';
                $output.='<th>Project Description</th>';
                $output.='<th>Time Frame</th>';
                $output.='<th>Team Leader</th>';
                $output.='<th>Volunteer Limit</th>';
                while (have_posts()) : the_post();

                        //Get post attendee amount
                        $project_id = get_the_ID();
                        $attendees = $wpdb->get_var("SELECT COUNT(user_id) FROM ".$wpdb->prefix."mro_attendees WHERE event_id = ".$project_id."");

                        //Get team leader name
                        $leader = $wpdb->get_results("SELECT user_first_name, user_last_name FROM ".$wpdb->prefix."mro_attendees WHERE event_id = ".$project_id." AND user_role = 'team_leader' LIMIT 0,1");
                        foreach ($leader as $lead) {
                        $leaderfirst = $lead->user_first_name;
                        $leaderlast = $lead->user_last_name;
                        $fullname = $leaderfirst . ' ' . $leaderlast;

                        //Title and link
                        $temp_title = get_the_title($post->ID);
                        $temp_link = get_permalink($post->ID);

                        // output all findings - CUSTOMIZE TO YOUR LIKING
                        $output.='<tr class="one-sixth">';
                        $output.='<td><a href="'.$temp_link.'">'.$temp_title.'</a></td>';

                $output.='</table><div class="clear-line"></div>';
                //pagination links
                $output.= wp_pagenavi();

                $output.='nothing found.';


The team leader $fullname should only show once according to the post id that the event id is equal to in the loop, however, the output results end up putting these people in other rows for projects (post ids) that do not share the same event id with the person. So they (the team leader names) appear in extra rows in the loop and I can not figure out why this is the case. The count query above that does not cause the same problem.

Thanks and great appreciation to whoever can offer me help!

  • is it the case that the projects it happens on don't have leader associated with them? or do all projects have a leader and some are outputting the wrong name?
    – Milo
    Commented Aug 13, 2014 at 22:06
  • They happen on ones that do not have them. Commented Aug 13, 2014 at 22:07

1 Answer 1


This is a php logic / variable issue. You output $fullname whether or not it has been set inside that iteration's foreach. So for projects that never enter the foreach to set $fullname, it still contains whatever the last iteration had set it to. Set it to an empty string before the foreach so it only contains a name if it enters the foreach.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.