0

I have a custom post type named "football_team" and this custom post has a post named Barcelona with id "post=7". I am using single-football_team.php template to show this post as:

<?php while ( have_posts() ) : the_post(); ?>
    <?php get_template_part( 'content-parts/content', 'team' ); ?>
<?php endwhile; ?>

In content-parts/content-team.php file I am using following codes:

<?php
$terms = get_terms( 'competition', array(
    'orderby'    => 'count',
    'hide_empty' => true,
) );
?>
<?php
foreach( $terms as $term ) {?>
  <section class="achievement-section clearfix">
    <?php $counter = 0; ?>
    <div class=" league-and-season clearfix">
    <div class="honour-league-name"> <?php echo $term->name; ?> </div>
      <?php
      $team = get_post_meta( get_the_ID(), 'football_team_team_name', true );
      $args = array(
        'post_type'  => 'football_league',
        'tax_query' => array(
          array(
          'taxonomy' => 'competition',
          'field' => 'slug',
          'terms' => $term
          ), 
        ),          
        'meta_query' => array(
            array(
              'key' => 'football_league_team_name',
              'value' => $team,
              'compare' => '='          
            ),
        ),
      );

    $fixture_query = null;
    $fixture_query = new WP_Query($args); ?>
      <div class="honour-season-name">
        <ul >
            <?php while ( $fixture_query->have_posts() ) : $fixture_query->the_post(); ?>
                <?php
            $champion_team = get_post_meta( get_the_ID(), 'football_league_team_name', true );
            $terms_competition = get_the_terms( get_the_ID(), 'competition' );
            $terms_season = get_the_terms( $post->ID , 'season' ); ?>
                <?php 
                      foreach ($terms_season as $object) {?>
                       <li> <?php echo  $object->name; ?></li> 
                      <?php } 
                   ?>
                <?php $counter++; ?>
          <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        </ul>
      </div>
    </div>
    <div class="achievement-times"><span><?php echo $counter; ?> </span></div>
  </section> 
<?php }
?>

In the above codes following line is the metabox of team name of football_team custom post:

$team = get_post_meta( get_the_ID(), 'football_team_team_name', true );

AND


Above codes shows following:Taxonomy

But the problem is:

  1. PREMIER LEAGUE
  2. BUNDESLIGA

  3. ITALIAN SERIE A

Above three Competition taxonomy terms are not associated with the post no:7(Barcelona). I think it showing all the terms due to get_term function and foreach loop.Is there any way to fetch only specific taxonomy term associated the post=7 or Barcelona? Any solution would be appreciated!

0

I used following codes and it is working perfectly.

<?php
$terms = wp_get_post_terms($post->ID, 'competition'); ?>
<?php
$count = count($terms);
if ( $count > 0 ) {
foreach( $terms as $term ) {?>
  <section class="achievement-section clearfix">
    <?php $counter = 0; ?>
    <div class=" league-and-season clearfix">
    <div class="honour-league-name"> <?php echo $term->name; ?> </div>
      <?php
      $team = get_post_meta( get_the_ID(), 'football_team_team_name', true );
      $args = array(
        'post_type'  => 'football_league',
        'tax_query' => array(
          array(
          'taxonomy' => 'competition',
          'field' => 'slug',
          'terms' => $term
          ), 
        ),          
        'meta_query' => array(
            array(
              'key' => 'football_league_team_name',
              'value' => $team,
              'compare' => '='          
            ),
        ),
      );

    $fixture_query = null;
    $fixture_query = new WP_Query($args); ?>
      <div class="honour-season-name">
        <ul >
            <?php while ( $fixture_query->have_posts() ) : $fixture_query->the_post(); ?>
                <?php
            $champion_team = get_post_meta( get_the_ID(), 'football_league_team_name', true );
            $terms_competition = get_the_terms( get_the_ID(), 'competition' );
            $terms_season = get_the_terms( $post->ID , 'season' ); ?>
                <?php 
                      foreach ($terms_season as $object) {?>
                       <li> <?php echo  $object->name; ?></li> 
                      <?php } 
                   ?>
                <?php $counter++; ?>
          <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        </ul>
      </div>
    </div>
    <div class="achievement-times"><span><?php echo $counter; ?> </span></div>
  </section> 
<?php }
}
?>

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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