wp query - Group and list posts by custom taxonomy

admin2025-01-08  3

I'm trying to list posts from a CPT named "exposition" and group them based on a custom taxonomy called "date-exposition" (which are meant to be years such as 2010, 2015, 2017,...) in the following form :

2019

  • post 1
  • post 2

2018

  • post 3
  • post 4

etc...

Closest code I managed to compile is this (it only shows one date/taxonomy-value and not the posts...) :

<?php
    // Get current Category
    $get_current_cat = get_term_by('name', single_cat_title('',false), 'category');
    $current_cat = $get_current_cat->term_id;


    // List posts by the terms for a custom taxonomy of any post type
    $post_type = 'exposition';
    $tax = 'date-exposition';
    $tax_terms = get_terms( $tax, 'orderby=name&order=ASC');
    if ($tax_terms) {
        foreach ($tax_terms  as $tax_term) {
            $args = array(
                'post_type'         => $post_type,
                "$tax"              => $tax_term->slug,
                'post_status'       => 'publish',
                'posts_per_page'    => -1,
                'category__in'      => $current_cat // Only posts in current category (category.php)
            );

            $my_query = null;
            $my_query = new WP_Query($args);

            if( $my_query->have_posts() ) : ?>

                <h2><?php echo $tax_term->name; // Group name (taxonomy) ?></h2>

                <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
                    <?php $term_list = wp_get_post_terms($post->ID, 'category', array("fields" => "ids")); // Get post categories IDs?>

                    <?php if (in_array($current_cat, $term_list) ): // Display only posts that have current category ID ?>
                        <h3><?php the_title(); ?></h3>
                    <?php endif; // if in_array ?>

                <?php endwhile; // end of loop ?>

            <?php endif; // if have_posts()
            wp_reset_query();

        } // end foreach #tax_terms
    } // end if tax_terms
?>

Thanks for help. Please note I am really not good at PHP.

I'm trying to list posts from a CPT named "exposition" and group them based on a custom taxonomy called "date-exposition" (which are meant to be years such as 2010, 2015, 2017,...) in the following form :

2019

  • post 1
  • post 2

2018

  • post 3
  • post 4

etc...

Closest code I managed to compile is this (it only shows one date/taxonomy-value and not the posts...) :

<?php
    // Get current Category
    $get_current_cat = get_term_by('name', single_cat_title('',false), 'category');
    $current_cat = $get_current_cat->term_id;


    // List posts by the terms for a custom taxonomy of any post type
    $post_type = 'exposition';
    $tax = 'date-exposition';
    $tax_terms = get_terms( $tax, 'orderby=name&order=ASC');
    if ($tax_terms) {
        foreach ($tax_terms  as $tax_term) {
            $args = array(
                'post_type'         => $post_type,
                "$tax"              => $tax_term->slug,
                'post_status'       => 'publish',
                'posts_per_page'    => -1,
                'category__in'      => $current_cat // Only posts in current category (category.php)
            );

            $my_query = null;
            $my_query = new WP_Query($args);

            if( $my_query->have_posts() ) : ?>

                <h2><?php echo $tax_term->name; // Group name (taxonomy) ?></h2>

                <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
                    <?php $term_list = wp_get_post_terms($post->ID, 'category', array("fields" => "ids")); // Get post categories IDs?>

                    <?php if (in_array($current_cat, $term_list) ): // Display only posts that have current category ID ?>
                        <h3><?php the_title(); ?></h3>
                    <?php endif; // if in_array ?>

                <?php endwhile; // end of loop ?>

            <?php endif; // if have_posts()
            wp_reset_query();

        } // end foreach #tax_terms
    } // end if tax_terms
?>

Thanks for help. Please note I am really not good at PHP.

Share Improve this question asked Feb 17, 2022 at 19:25 TonioLipTonioLip 235 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I re-factored your code, there were quite some parts which seemed overly complex or used deprecated methods.

In the code below, I'm looping through your terms, and within that loop, I'm finding the posts relating to that term. Probably there's a more efficient way, since this is using several queries, but it should achieve the desired result.

<?php
// get the terms, ordered by name
// https://developer.wordpress.org/reference/functions/get_terms/
// https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
$taxonomy = 'date-exposition';
$tax_terms = get_terms( 
  array(
    'taxonomy' => $taxonomy,
    'hide_empty' => false, // change to true if you don't want empty terms
    'orderby' => 'name',
    'order' => 'DESC',
    'fields' => 'names', // return the term names only
  )
);

foreach($tax_terms as $tax_term) { // loop through the terms
  echo '<h2>' . $tax_term . '</h2>'; // echo the term name as a h2
  $term_posts = get_posts( // find posts with the correct term
    array(
      'no_found_rows' => true, // for performance
      'ignore_sticky_posts' => true, // for performance
      'post_type' => 'exposition',
      'posts_per_page' => -1, // return all results
      'tax_query' => array( // https://developer.wordpress.org/reference/classes/wp_tax_query/
        array(
              'taxonomy' => $taxonomy,
              'field'    => 'name',
              'terms'    => array( $tax_term )
          )
      ),
      'fields' = 'ids', // return the post IDs only
    )
  );
  
  echo '<ul>'; // open bullet list
  foreach($term_posts as $term_post_id) { // loop through posts
    $post_title = get_the_title($term_post_id); // get post title
    $post_permalink = get_the_permalink($term_post_id); // get post link
    echo '<li>'; // open list item
    echo '<a href="' . $post_permalink . '">' . $post_title . '</a>'; // add link to post with post title as link text
    echo '</li>'; // close list item
  }
  echo '</ul>'; // close bullet list
}
?>

https://codeshare.io/Jbyqvd

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266584a1133.html

最新回复(0)