Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create a table containing information that is a result of two different loops. The first loops through a list of custom post types to display team names. The other is to provide data concerning each of the teams and stored as metadata. So I think I need some way of constructing a double query in order to create a single loop. This is what I have so far:

$team = new WP_Query(array('post_type' => 'team_page') ); 

$args = array(
       'post_type' => 'match_report',
       'meta_query' => array(
         'relation' => 'OR',
          array(
             'key' => 'report_home-select',
             'value' => $team->post->ID,
             'compare' => '=',
          ),
          array(
             'key' => 'report_away-select',
             'value' => $team->post->ID,
             'compare' => '='
          )
       )                                  
     );

    $my_query = new WP_Query($args); 
    $played = $my_query->post_count;

if ( $team->have_posts() ) {
while ( $team->have_posts() ) {
    $team->the_post();
?>
                    <tr>

                        <td class="teamname"><?php the_title(); ?></td>

                        <td><?php echo $played;?></td>

                        <td>9</td><!-- won -->

                        <td>0</td><!-- draw -->

                        <td>2</td><!-- lost -->

                        <td class="goalinfo"></td><!-- goals for -->

                        <td class="goalinfo">10</td><!-- goals against -->

                        <td class="goalinfo">33</td><!-- goal difference -->

                        <td class="total">27</td><!-- goal total -->
                    </tr>

 <?php } ?>
share|improve this question
add comment

2 Answers

The biggest problem here is syncing between two dissimilar sets of data. This looks like a good use-case for @scribu 's Posts2Posts plugin.

Once you've installed that, you can create a direct correlation between the different post-types and query that connection.

share|improve this answer
    
I looked into it and while it does look great I suspect there is a lot of room for error as the person filling in the data could easily forget to make the connections. Also the team ID is the common denominator between the two custom post types. –  mantis Jun 16 '13 at 21:36
    
Ah, I see. thank you. The problem is that there are two teams in each match report and I need a list of teams for when there are no matches. I think I might try the $wpdb object. –  mantis Jun 16 '13 at 22:08
    
I deleted my edit because I don't understand your schema enough to provide a worthwhile answer. There has to be a way to pull the info in one query and sort by team ID. –  sprclldr Jun 16 '13 at 22:16
    
There probably is but I can't quite wrap my head around it using WP_Query. I'll sleep on it tonight and see if it makes more sense in the morning. Thanks for your help. –  mantis Jun 16 '13 at 22:45
add comment
up vote 0 down vote accepted

I did mange to find the answer to this so I'll leave it here for posterity (and because I'm very pleased with myself ;) )

$team = new WP_Query(array('post_type' => 'team_page') ); 
$i=0;

if ( $team->have_posts() ) {
while ( $team->have_posts() ) { 
    $team->the_post();

$args = array(
       'post_type' => 'match_report',
       'meta_query' => array(
         'relation' => 'OR',
          array(
             'key' => 'report_home-select',
             'value' => $team->posts[$i]->ID,
             'compare' => '=',
          ),
          array(
             'key' => 'report_away-select',
             'value' => $team->posts[$i]->ID,
             'compare' => '='
          )
       )                                  
     );

    $reports = new WP_Query($args); 
    $played = $reports->found_posts;
    $i++;       
?>  

The biggest problem was that the second query was outside of the loop. I hope this is helpful to someone.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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