WordPress.org

Ready to get started?Download WordPress

Forums

List all posts in current category and group by year (4 posts)

  1. Andy
    Member
    Posted 3 years ago #

    Hey guys wondering if someone can help me out

    I have a client who wants me to list all of the posts in a category and group them by the year they were published.

    Example:

    2011
    - Post title
    - Post title
    2010
    - Post title
    - Post title

    Any ideas how to do this?

    I tried wp_get_archives but it only listed the year and I could not list the posts underneath the year

    Thanks

    Andy

  2. doc4
    Member
    Posted 3 years ago #

    AndyT81,

    Here is a plugin you might be interested in:
    http://blog.favrik.com/2007/10/26/wordpress-plugin-recent-posts-grouped-by-date/

  3. Andy
    Member
    Posted 3 years ago #

    Hey Doc4

    Thanks for that. After deleting out the day and month from the plugin code it does exactly what I want except limit the posts to one category.

    Do you know how to just show posts from a select category? I tried adding 'category' => 'nta' to the array but no luck.

    Thanks again

    Andy

  4. Andy
    Member
    Posted 3 years ago #

    Anyone have any ideas on how to limit the posts to one category?

    Here is the modified plugin code I am using but it pulls all posts from the site and I only want one particular category:

    <?php
    
    function favrik_recent_posts($args = '') {
    	global $wp_locale, $wpdb;
    
        // params fun
        parse_str($args, $r);
        $defaults = array('category_name' => 'NTA', 'group' => '1', 'limit' => '10', 'before' => '<li>', 'after' => '</li>', 'show_post_count' => false, 'show_post_date' => true, 'date' => 'F jS, Y', 'order_by' => 'post_date DESC');
    	$r = array_merge($defaults, $r);
    	extract($r);
    
        // output
        $output    = '';
        $pre       = '';
        $full_date = '';
        $year      = '';
        $month     = '';
        $day       = '';
    
    	// the query
    	$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'");
    	$join  = apply_filters('getarchives_join', "");
        $qry   = "SELECT ID, post_date, post_title, post_name
                  FROM $wpdb->posts $join
                  $where ORDER BY $order_by LIMIT $limit";
    	$arcresults = $wpdb->get_results($qry);
    	if ($arcresults) {
    		foreach ($arcresults as $arcresult) {
    			if ($arcresult->post_date != '0000-00-00 00:00:00') {
    				$url  = get_permalink($arcresult);
    				if ($group == 0) { // dates at the side of the post link
                        $arc_date = date($date, strtotime($arcresult->post_date));
                        $full_date = '<em class="date">' . $arc_date . '</em> ';
                    }
                    if ($group == 1) { // grouping by year then month-day
                        $y = date('Y', strtotime($arcresult->post_date));
                        if ($year != $y)  {
                            $year = $y;
                            $pre = '<li class="year">' . $year . '</li>';
                        }
                    }
                    $text = strip_tags(apply_filters('the_title', $arcresult->post_title));
    				$output .= get_archives_link($url, $text, $format,
                                                  $pre . $before . $full_date,
                                                 $after);
                    $pre = ''; $full_date = '';
    			}
    		}
        }
        echo $output;
    }
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic