0

I want to group my custom post types into years.

I should look like

2019 Post 1 Post 2 Post 3

2018 Post 1 Post 2

2015 Post 1

YEAR Post 1..n

Following this example here: Order and group posts by acf by month and year I've set up my code like this.

<?php get_header(); ?>
<?php /* Template Name: Konzerte */ 
// Überschrift-Element mit Headline, Bild, Schwung und Text; 800px hoch
require('inc/stage.php'); ?>
<?php 
// get posts
$posts = get_posts(array(
'post_type'         => array('concertreport', 'concertannouncement'),
'posts_per_page'    => -1,
'meta_key'          => 'concertdate',
'orderby'           => 'meta_value',
'order'             => 'DESC'
)); 
?>


<?php
    $group_posts = array();

        if( $posts ) {
            foreach( $posts as $post ) {
                $date = get_field('concertdate', $post->ID, false);
                $date = new DateTime($date);
                $year = $date->format('Y');
                $group_posts[$year][] = array($post, $date);
            }

        }
        foreach ($group_posts as $yearKey => $years) { ?>

    <h2><?php echo $yearKey; ?></h2>
    <?php foreach ($years as $postKey => $posts) { ?>
      <?php echo $posts[1]->format('d-m-Y');
          echo ' / ';
          echo $posts[0]->post_title;
          echo '<br>';
          // Instead of "echo $posts[0]->post_title;" I want to display the ACF-(Sub-)fields like teaser-text, image, etc. 

        } 
    }
?>

<?php get_footer(); ?>

Result, which is really fine:

Posts grouped by year the right way

As you can see in my comment, I'd like to show the ACF fields there. Custom image, custom teaser-text, etc. I don't know why, if I copy my code from ACF there, for example the title instead of the comment:

<div class="col-md-7 bg-wso-white">
  <?php the_title( '<h3 class="font-weight-bold text-left text-wso-black text-uppercase pb-1"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h3>' ); ?>
</div>

Result, which isn't so fine ;-):

enter image description here

It only displays the text of the oldest date repeatedly for all posts. My acf sub-fields doesn't display at all. Can anybody help me?

1 Answer 1

0

The reason you can't use ACF or functions such as the_title() is because they can only be accessed from inside The Loop (see here https://codex.wordpress.org/The_Loop).

Its unnecessary to have multiple loops so I've merged them into one which will display any post data you need. Within this loop it checks whether the current post's year is equal to the previous post's year before it displays it.

<?php /* Template Name: Konzerte */
// Überschrift-Element mit Headline, Bild, Schwung und Text; 800px hoch
require('inc/stage.php');

    global $post;

    // get posts
  $posts = get_posts(array(
      'post_type'         => array('concertreport', 'concertannouncement'),
      'posts_per_page'    => -1,
      'meta_key'          => 'concertdate',
      'orderby'           => 'meta_value',
      'order'             => 'DESC'
  ));

  $group_posts = array();

    if ($posts) {
      $previous_year = '';
      foreach ($posts as $post) {
        $date = get_field('concertdate', $post->ID, false);
        $date = new DateTime($date);
        $current_year = $date->format('Y');

        // If $previous_year is empty, display $current_year
        if ($previous_year == '') {
            echo '<h2>' . $current_year . '</h2>';
        }
        // Else if $previous_year is not equal to $current_year, display $current_year
        else if ($previous_year !== $current_year) {
            echo '<h2>' . $current_year . '</h2>';
        }

        $previous_year = $date->format('Y');
        echo $date->format('d-m-Y');
        echo ' / ';
        echo get_the_title();
        echo '<br>';
      }
    }
?>

<?php get_footer(); ?>
2
  • Thank you so much for this help! That was the solution!
    – nullstein
    Jul 30, 2019 at 20:38
  • No problem - Happy it's fixed!
    – Louis S
    Jul 31, 2019 at 8:36

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