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
<?php
// the loop
if (have_posts()) :
$output.='<table class="contentTable" border="0" width="100%">';
$output.='<tr class="one-sixth">';
$output.='<th>Agency</th>';
$output.='<th>Project Description</th>';
$output.='<th>Time Frame</th>';
$output.='<th>Team Leader</th>';
$output.='<th>Volunteer Limit</th>';
$output.='</tr>';
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.='<td>'.get_custom_field('project_description').'</td>';
$output.='<td>'.get_custom_field('project_timeframe').'</td>';
$output.='<td>'.$fullname.'</td>';
$output.='<td>'.$attendees.'/'.get_custom_field('project_limit').'</td>';
$output.='</tr>';
endwhile;
$output.='</table><div class="clear-line"></div>';
//pagination links
$output.= wp_pagenavi();
$output.='<style>#pagenavi{display:none;}</style>';
$output.='<style>.wp-pagenavi{margin-bottom:20px;}</style>';
else:
$output.='nothing found.';
endif;
?>
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!