0

I have a CPT named “football_fixture”.This CPT has taxonomy named “competition”, and this taxonomy has different terms , laliga, eng. I want to show my post as follow:
laliga:
List of all posts published including above terms.
eng:
List of all posts published including above terms.
I am using following codes but it show the content as showed in the image1. enter image description here

I want the post like image 2: enter image description here image1: Here is the codes I am using:

<?php
/**
* Template Name: Fixture
* Description: The template for displaying all  posts and attachments
*/
?>
<?php get_header(); ?>
<?php


$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$term_id = get_queried_object()->term_id;

$args = array( 'post_type' => 'football_fixture',
                'paged' => $paged,

            );

    query_posts( $args );
?>

<?php if(have_posts()): ?>
    <div class="<?php echo $col; ?>">


        <?php while(have_posts()): ?>
            <?php the_post(); ?>

            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                 <?php
                    $id         = get_the_ID();
                    $date       = rwmb_meta( 'pb_match_date','', $post->ID);
                    $time       = rwmb_meta( 'pb_match_time','', $post->ID );
                    $competition = rwmb_meta( 'pb_match_competition_cats','', $post->ID );
                    $team_a     = get_post_meta( $post->ID, 'match_details_home_team', true );
                    $team_b     = get_post_meta( $post->ID, 'match_details_away_team', true );

                ?>
                <div class="fixture-item">
                    <div class="fixture-info clearfix">
                        <p class="pull-left match-date"><?php echo ($date); ?></p>
                        <p class="pull-left match-date">
                        <?php 

                        $terms = get_the_terms( get_the_ID(), 'competition' ); // 'taxonomy' field doesn't store term IDs in the custom fields, instead, it sets post terms
                        if ( !empty( $terms ) ) {
                            $content = '<ul>';
                            foreach ( $terms as $term ) {
                                $content .= sprintf(
                                    '<li><a href="%s" title="%s">%s</a></li>',
                                    get_term_link( $term, 'tax_slug' ),
                                    $term->name,
                                    $term->name
                                );
                            }
                            $content .= '</ul>';
                            echo $content;
                        }
                        ?>

                        </p>
                    </div>

                    <div class="row">
                        <div class="col-xs-4">
                            <div class="media">

                                <div class="media-body">
                                    <h4><?php echo $team_a; ?></h4>
                                </div>
                            </div>
                        </div>

                        <div class="col-xs-4 match-time">
                            <i class="fa fa-clock-o"></i> <?php echo $time; ?>
                        </div>

                        <div class="col-xs-4">
                            <div class="media">

                                <div class="media-body">
                                    <h4 class="pull-right"><?php echo $team_b; ?></h4>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div><!--/#post-->
        <?php endwhile; ?>

        <?php 
            wp_reset_query();
        ?>

    </div>
    <?php endif; ?>

<?php get_footer(); ?>
0

In your custom templates, First you have to fetch all terms for your custom post type.

You can use get_terms to retrive terms in taxonomy

If you are using wordpress prior to 4.5

$terms = get_terms( 'competition' , array(
    'hide_empty' => false
) );

OR if you are using wordpress prior to 4.5.0 or later

$terms = get_terms( array(
    'taxonomy' => 'competition',
    'hide_empty' => false,
) ); 

Next Build Term name lists:

$termNames = array();
$count = count( $terms );
if ( $count > 0 ) {
    foreach ( $terms as $term ) {
        $termNames[] = $term->name;
    }
}

Next loop trough each terms and query posts for the current terms & then run the loop:

foreach($termNames as $termName) :

$args = array(
    'post_type' => 'football_fixture',
    'competition' => $termName,
    'order' => 'ASC',
);
query_posts( $args );
if(have_posts()): ?>
    <div class="<?php echo $col; ?>">
        <h2><?php echo $termName; ?></h2>
        <?php while(have_posts()):  the_post(); ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
             <?php
                $id         = get_the_ID();
                $date       = rwmb_meta( 'pb_match_date','', $post->ID);
                $time       = rwmb_meta( 'pb_match_time','', $post->ID );
                $competition = rwmb_meta( 'pb_match_competition_cats','', $post->ID );
                $team_a     = get_post_meta( $post->ID, 'match_details_home_team', true );
                $team_b     = get_post_meta( $post->ID, 'match_details_away_team', true );

            ?>
            <div class="fixture-item">
                <div class="row">
                    <div class="col-xs-4">
                        <div class="media">
                            <div class="media-body">
                                <h4><?php echo $team_a; ?></h4>
                            </div>
                        </div>
                    </div>

                    <div class="col-xs-4 match-time">
                        <i class="fa fa-clock-o"></i> <?php echo $time; ?>
                    </div>

                    <div class="col-xs-4">
                        <div class="media">
                            <div class="media-body">
                                <h4 class="pull-right"><?php echo $team_b; ?></h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div><!--/#post-->
    <?php endwhile; ?>

    <?php 
        wp_reset_query();
    ?>

| improve this answer | |
1

You need to query for your posts which have a the associated custom taxonomy term(s),

$args = array(
  'post_type' => 'football_fixture',
  'tax_query' => array(
      array(
        'taxonomy' => 'competition',
        'field'    => 'term_id',
        'terms'    => array($term_id),  //put more term ids if required
      ),
    ),
  );
$query = new WP_Query( $args );
if($query->have_posts()){
  while($query->have_posts()){
    $query->the_post();
    echo '<li>' . get_the_title( $query->post->ID ) . '</li>';
  }

  // Restore original Post Data once finished, IMPORTANT
  wp_reset_postdata();
}

now, you need to get the $term_id of your term to make sure the above query works, in your example you have the following line at the top,

$term_id = get_queried_object()->term_id;

however, the function get_queried_object will only return the the term_id for a taxonomy template, what you are trying to do is display this in a page template, as your page header starts with,

 <?php
 /**
 * Template Name: Fixture    -- this indicates that you are building a page template

so you will not have access to any term id. You have 2 options to get the results you are looking for,

  • hard code the term ID in your page template, or pass it as an argument in the URL attribute
  • create a taxonomy archive template, so you can use your custom taxonomy terms directly into your menu, users will click the term link and WordPress will parse the term id to your page.

Putting it all together,

I would recommend the second option. Create a new file called taxonomy-competition.php and save it in your root theme or child theme folder with the following content,

<?php
/**
 * Taxonomy 'competition' archive template
 */
get_header();

$term_id = get_queried_object()->term_id;
$args = array(
  'post_type' => 'football_fixture',
  'tax_query' => array(
      array(
        'taxonomy' => 'competition',
        'field'    => 'term_id',
        'terms'    => array($term_id),  //put more term ids if required
      ),
    ),
  );
$query = new WP_Query( $args );

echo '<ul>';
if($query->have_posts()){
  while($query->have_posts()){
    $query->the_post();
    echo '<li>' . get_the_title( $query->post->ID ) . '</li>';
  }

  // Restore original Post Data once finished, IMPORTANT
  wp_reset_postdata();
}
echo '</ul>';

get_footer();

This will display the list post titles which are organised under the given term.

To use this on the front end, add the custom taxonomy competition terms you want to display your navigational menu in the dashboard. If you are unable to see your taxonomy menu option, make sure it is selected in the 'Screen Options' tab at the top of the dashboard page.

| improve this answer | |

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.