3

Okay, so I'm making the blog page for my WordPress site. My front page is displayed as static, so my blog posts are on another page called "Journal." I want to display the posts in an archive-like fashion. I'm trying to achieve the following:

I researched how to do this, and I found a way; however, it displays the dates for all the posts as "November 30."

This is the code I'm using:

                <?php
                // Get years that have posts
                $years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY year ASC" );

                //  For each year, do the following
                foreach ( $years as $year ) {

                    // Get all posts for the year
                    $posts_this_year = $wpdb->get_results( "SELECT ID, post_title FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );

                    // Display the year as a header
                    echo '<h1>' . $year->year . '</h1>';

                    // Start an unorder list
                    echo '<ul class="posts-in-year">';

                    // For each post for that year, do the following
                    foreach ( $posts_this_year as $post ) {
                        // Display the title as a hyperlinked list item
                        echo '<li><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a>' . ' ' . '&mdash;' . ' ' .  get_the_time('F j') . '</li>';
                    }

                    // End the unordered list
                    echo '</ul>';
                }
                ?> 

Basically, I'm trying to get the time from each post and display its month and day (F j)

5
  • 1
    It's because you're not using it in a WordPress loop Jun 23, 2015 at 23:56
  • @MannyFleurmond I put it in a loop, and it just outputs the same thing twice.
    – Matthew
    Jun 24, 2015 at 0:00
  • You aren't putting it in a WP_Query loop. get_the_time is only designed to work either in the main WordPress loop or a custom loop using WP_Query Jun 24, 2015 at 0:02
  • @MannyFleurmond Oh, okay. What would I query? $years? I'm unfamiliar with $wpdb.
    – Matthew
    Jun 24, 2015 at 0:12
  • Just posted an answer with the proper usage of get_the_time Jun 24, 2015 at 0:12

1 Answer 1

6

The function get_the_time is only designed to work either in the main WordPress loop or a custom loop using WP_Query, so the way you are using it won't work correctly. The following is the right way to use it, using a custom WP_Query loop:

<?php
// Get years that have posts
$years = $wpdb->get_results( "SELECT DISTINCT YEAR(post_date) AS year FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY year ASC;" );

//  For each year, do the following
foreach ( $years as $year ) {    
    // Get all posts for the year.  Using WP_Query instead of custom mySQL
    $posts_this_year = new WP_Query( array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'year' => $year->year
    ) );    

    // Display the year as a header
    echo '<h1>' . $year->year . '</h1>';

    // Start an unorder list
    echo '<ul class="posts-in-year">';

    // As long as you have posts for that year, do the following.  Using the Loop.  get_the_date now works
    while ( $posts_this_year->have_posts() ) {
        //Makes current post available to template tag functions like get_the_time
        $posts_this_year->the_post(); 
        // Display the title as a hyperlinked list item
        echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a>' . ' ' . '&mdash;' . ' ' .  get_the_time('F j') . '</li>';
    }

    //Reset post data.  Important to do this so not to mess with main loop
    wp_reset_postdata();

    // End the unordered list
    echo '</ul>';
}

?>

The Loop, which I was referring to earlier, is when you use WP_Query to iterate through the posts for you. When you do it that way, functions like get_the_time work properly because they can pull the info for the current post in the loop. For more information, read The codex data on WP_Query and multiple loops

3
  • Edited with some info to how and why this works. Jun 24, 2015 at 0:32
  • SELECT DISTINCT YEAR(post_date) AS year FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY year ASC; is 40 times faster on my machine. Also, a date query with all the years might save more queries.
    – fuxia
    Jun 24, 2015 at 0:54
  • @toscho was just reusing his code. Thanks for the catch. Jun 24, 2015 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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