I am working with two custom post types on my site: Team and League. I am creating a page template for the CPT League that will display meta from all of the 'Team' posts for team with a meta value matching the name of the League post type. The query will look through CPT 'team' for the meta value 'team_league'. The value in 'team_league' will match one of the post IDs for the 'league' type. I would then like to print each matching 'team', along with some meta from the team post, on the 'league' page.
I can successfully do this if I print a simple with just the name of the posts found in the query. The example below successfully queries and prints the post name of each 'team' with a meta value matching the 'league' title. However, I need something far more complicated. I want to pull meta from the 'team' post and print a table containing names from the 'team' CPT.
Working, Simplified Query:
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
Desired Query, Currently Doesn't Work:
<?php
/*
Template Name: League Layout
Template Post Type: league
*/
$leaguename = get_the_ID();
$args = array(
'meta_key' => 'team_league',
'meta_value' => $leaguename,
'post_type' => 'team',
'post_status' => 'any',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//Get the team data
$post_status = get_post_status();
$team_name = get_the_title();
$captain_id = get_post_meta(get_the_ID(), 'captain_user_id', true);
$team_captain = get_user_by( 'ID', $captain_id );
$team_league = get_post(get_post_meta(get_the_ID(), 'team_league', true));
$team_players = get_post_meta(get_the_ID(), 'team_players_emails', true);
$team_paid = get_post_meta(get_the_ID(), 'current_paid_amount', true);
$team_fee = get_post_meta(get_the_ID(), 'payment_product_price', true);
$team_balance = round( $team_fee - $team_paid , 2);
echo '<p>Team Captain: <b>' . $team_captain->first_name . ' ' . $team_captain->last_name; . '</b></p>';
echo '<p>Team Balance: <b>$' . $team_balance; . '</b></p>';
echo '<table>';
echo '<tr>';
echo '<th>Team Roster</th>';
echo '</tr>';
$users_confirmed = 0;
foreach ($team_players as $team_player) {
$user = get_user_by( 'email', $team_player['email'] );
if( $user ){
if( !$user->first_name && !$user->last_name ){
continue;
}
echo sprintf( "<tr><td>%s</td></tr>", $user->first_name . ' ' . $user->last_name );
$users_confirmed++;
}
}
if( 0 == $users_confirmed ){
echo sprintf( "<tr><td>%s</td></tr>", "No confirmed players for this team" );
}
echo '</table>';
}
}
else {
echo '<p>No Teams in this league</p>';
}
/* Restore original Post Data */
wp_reset_postdata();
For reference, the following solution works to achieve what I was aiming for above. The main change is to the args, where I had to include custom post status for my CPTs:
$leagueid = get_the_ID();
$args = array(
'post_type' => 'team',
'posts_per_page' => - 1,
'post_status' => array( 'mssl-completed', 'mssl-pending', 'publish' ),
'meta_query' => array(
array(
'key' => 'team_league',
'value' => "$leagueid",
),
),
);
From here I was able to write the information pulled from the post meta to my custom page template.