skip to Main Content

I’m trying to create an archive page for my blog posts that is sorted by the date that is added through an ACF datepicker-field.

This is what I have so far:

$args = array(
'post_type'         =>  'post',
'posts_per_page'    =>  -1,
'orderby'           =>  'meta_value',
'order'             =>  'DESC',
'meta_key'          =>  'datum_artikel',
'meta_value'        =>  date('Ymd',strtotime("today")),
'meta_compare'      =>  '<='
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    echo "<div class='posts'>";

        while ( $query->have_posts() ) {
            $query->the_post();

            $f = get_fields();
            $link = ($f['bericht_doorverwijzen'] ?? '' ? $f['bericht_doorverwijzen'] : get_the_permalink());
            $img_id = get_post_thumbnail_id();

            $date = $f['datum_artikel'];

            echo "<div class='post w-1/3'>";
                echo "<div class='postWrapper'>";

                    if(!empty($img_id)) {
                        $img = Images::get_image_resized($img_id, 397, 230, true);
                        echo "<a href='".$link."' title='Ga naar ".get_the_title()."'>";
                            echo '<picture class="img">';
                                echo '<img src="'.$img[0].'" alt="'.get_post_meta($img_id, '_wp_attachment_image_alt', true).'"/>';
                            echo '</picture>';
                        echo "</a>";
                    }

                    echo "<div class='content'>";
                        echo "<h2><a href='".$link."' title='Ga naar ".get_the_title()."'>".get_the_title()."</a></h2>";
                        if(!empty($date)) {
                            echo "<p class='date'>". $date ."</p>";
                        }
                        echo the_excerpt_max_charlength($charlength = 130);
                    echo "</div>";
                echo "</div>";
            echo "</div>";
        }
    echo "</div>";
}
wp_reset_postdata();

I found some examples online but none of them seem to work with my code. How can I add the name of the month between the posts when the month changes and can I create an extra navigation with all the months and an anchor so it jumps to the right posts?

2

Answers


  1. Chosen as BEST ANSWER

    I found another example online that seems to do what what I need:

    $query = new WP_Query( array(
    'post_type'     => 'post',
    'post_status'   => 'publish',
    'meta_key'      => 'datum_artikel',
    'orderby'       => 'meta_value',
    'order'         => 'DESC',
    ) );
    
    if ( $query->have_posts() ) {
        $current_header = "";
    
        echo "<div class='posts'>";
    
            while ( $query->have_posts() ) {
                $query->the_post();
                $f = get_fields();
                $date = $f['datum_artikel'];
                $temp_date = get_post_meta( get_the_ID(), 'datum_artikel', true);
                $pretty_month = date_i18n("F Y", strtotime($temp_date));
    
                if ($pretty_month != $current_header) {
                    $current_header = $pretty_month;
                    echo "<h2>". $pretty_month ."</h2>";
                }
    
    
                $link = ($f['bericht_doorverwijzen'] ?? '' ? $f['bericht_doorverwijzen'] : get_the_permalink());
                $img_id = get_post_thumbnail_id();
    
                echo "<div class='post w-1/3'>";
                    echo "<div class='postWrapper'>";
    
                        if(!empty($img_id)) {
                            $img = Images::get_image_resized($img_id, 397, 230, true);
                            echo "<a href='".$link."' title='Ga naar ".get_the_title()."'>";
                                echo '<picture class="img">';
                                    echo '<img src="'.$img[0].'" alt="'.get_post_meta($img_id, '_wp_attachment_image_alt', true).'"/>';
                                echo '</picture>';
                            echo "</a>";
                        }
    
                        echo "<div class='content'>";
                            echo "<h2><a href='".$link."' title='Ga naar ".get_the_title()."'>".get_the_title()."</a></h2>";
                            if(!empty($date)) {
                                echo "<p class='date'>". $date ."</p>";
                            }
                            echo the_excerpt_max_charlength($charlength = 130);
                            echo "<a href='".$link."' title='Ga naar ".get_the_title()."' class='link'>Lees meer</a>";
                        echo "</div>";
                    echo "</div>";
                echo "</div>";
            }
            echo "</div>";
    }
    wp_reset_postdata();
    

  2. $today = date("Y/m/j");
    $args = (array(
        'post_type' => 'post',
        'posts_per_page'    =>  -1,
        'meta_key' => 'datum_artikel',
        'orderby' => 'meta_value',
        'order' => 'DESC',
        'meta_query' => array(
            array(
                'key' => 'datum_artikel',
                'value' => $today,
                'compare' => '<=',
                'type' => 'CHAR'
            )
        )
    ));
    $query = new WP_Query($args); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search