WordPress.org

Ready to get started?Download WordPress

Forums

Group posts by month ? (7 posts)

  1. Ruben S.
    Member
    Posted 5 years ago #

    Hi, is there a way to do this ?

    August 2009
    11 Title of the post of the 11th day of August
    15 Title of other post of the 15th day of August
    20 Title of another post of the 20th day of August

    July 2009
    4 Title of the post of the 4th day of July
    7 Title of other post of the 7th day of July
    15 Title of another post of the 15th day of July

    Thanks!

  2. kittywake
    Member
    Posted 4 years ago #

    I have something similar on my blog - but I don't show the date of each post: just the title.

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>				<ul >
    <?php
    /**/
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date ASC");
    foreach($years as $year) :
    ?>
    
    	<h4><li><a href="<?php echo get_year_link($year ); ?> "><?php echo $year; ?></a>
                    <ul>
    		<?php	$months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");
    			foreach($months as $month) :
    		?>
    			<h4><li><a href="<?php echo get_month_link($year, $month); ?>"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>
    
    		      <ul>
                              <?php  $theids = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND MONTH(post_date)= '".$month."' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");
                          		foreach ($theids as $theid):	
    
    			   ?>
                                    <h4 style= font-style:italic;><li><a href="http://www.kittywake.us/?p=<?php echo $theid->ID; ?>"><?php echo $theid->post_title; ?></a></li></h4>                            
    
    			<?php
    				endforeach; ?>
    
    			</ul>				  
    
    	                </li></h4>
    
                             <?php endforeach;?>
                  </ul>
    
            </li></h4>
    <?php endforeach; ?>
           </ul>
    
    			</div>
    </div>

    The only problem I have with this is that if someone clicks on the month name the posts are in descending order while the listing is in ascending. You might want to change the code to all DESC so that you don't have this happen. I'm still working on a solution.

  3. design_dolphin
    Member
    Posted 4 years ago #

    Maybe this will help as well.

    I was looking for a way to group posts in categories automatically by day of the month they were posted and ordered by year. I am not sure if it will work on somebody else blog. You would have to work on the code if you want to change it to month only. The code works in category.php (It might work on category-(*).php or other theme template files as well, but I haven't tested this).

    I tried your code first @kittywake, thank you for posting it, it worked like charm. I was looking for something to select on category automatically, and trying to figure out the inner join for that as well, regardless of your code, appeared to be quite daunting. Maybe it is possible to do it with an inner join, however I haven't been able to figure that out, yet.

    The following code will semi-automatically select the year based on the value of $groups[0]. So $groups[1] will select a different year than $groups[0]. I have tested this with two years of blog posts, not sure how many years it will work with. The goal is to have it do the year part automatically, but this is proving to be quite difficult so far.

    The url's and the styling still needs to be added, but as this is somewhat personal preference I've left it as blanc as possible.

    @kittywake: Maybe this set-up will solve your problem with clicking on the month name, as well.

    <?php
         foreach (get_the_category() as $cat) {
         $arch_date_id = $cat->cat_ID;
         }
    
         global $post;
         $query = 'numberposts=-1&category=' . $arch_date_id . '&orderby=date&order=DESC';
         $myposts =  get_posts();
              foreach($myposts as $post) {
                   $year = get_the_time('Y');
                   $groups[] = $year;
         }
    
         $groups = array_values(array_unique($groups));
         $qi = $groups[0]; // change this number (e.g. to 1) to change the year.
         $query = 'cat=' . $arch_date_id . '&year=' . $qi . '&orderby=date&order=DESC&posts_per_page=-1';
         query_posts($query);
         echo '<h4>' . $qi . '</h4>';
         while (have_posts()) : the_post();
         ?> 
    
    <? the_date('F j','<h5>','</h5>');?>
    <? the_title();?><br />
    <? endwhile; ?>

    Feel free to improve.

    Code has been written for WordPress 2.8.6

  4. design_dolphin
    Member
    Posted 4 years ago #

    This does the almost the same thing as the code in my post above except that it now does so for custom taxonomies terms.

    The query_posts and get_posts will check which custom taxonomy and taxonomy term the taxonomy page is on. Then show by selected year the date and posts for that custom taxonomy term. The date is retrieved automatically from the database. For now one still has to select the year (please see the description in my previous post for this.)

    This code snippet is built to be used in taxonomy.php, and will only work with custom taxonomies.

    Please see my previous post for more details.

    <?php
    	$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    	$tt = $term->taxonomy . '=' . $term->slug;
    	global $post;
    	$query = 'numberposts=-1&' . $tt . '&orderby=date&order=DESC';
    	$myposts =  get_posts();
    	foreach($myposts as $post) {
    		$year = get_the_time('Y');
    		$title= $title= the_title('','',0);
    		$groeps[] = $year;
    	}
    
    	$groeps = array_values(array_unique($groeps));
    	$qi = $groeps[0]; // change this number (e.g. to 1) to change the year.
    	$query = 'posts_per_page=-1&' . $tt . '&year=' . $qi .  '&orderby=date&order=DESC';
    	query_posts($query);
    	echo '<h4>' . $qi . '</h4>';
    	while (have_posts()) :
    	the_post();
    	?>
     <?php the_date('F j','<h5>','</h5>'); ?>
     <?php the_title(); ?><br />
    <?php endwhile; ?>

    Feel free to improve.

    Code has been written for WordPress version 2.8.6.

  5. Kahil
    Member
    Posted 4 years ago #

    hey guys...

    I've been trying to do something similar for about a week now and get figure out the right combo of code pieces... I want to group posts by tags rather than date. Where all posts within any category archive I go to will be grouped by tags.

    For instance... I am in an archive for the category of Cheese. As it should, it would show all the posts in that category, but they are grouped by their tag (each post would have only a single tag for simplicity). Say... the first few posts listed have the tag of Mouse and the following posts listed have the tag of Rat.

    Make sense?

    I have attempted using multiple loops trying to get each loop to only display posts for a specific tag. I tried using queries and conditions and combos of both, etc...doesn't ever seem to work... :/

    any help would be greatly appreciated.

  6. Kahil
    Member
    Posted 4 years ago #

    I figured it out on my own. If anyone is interested, here is a sample of the solution.

    At the top of my template, before any loops, I have the following code to check to see what category the page I am on is and return its ID.

    <?php foreach(get_the_category() as $category)
          { $thecat = $category->cat_ID; } ?>

    Then, at the start of each loop I have the following. This code is set to call specific, constant post tags in respect to the particular category archive I am on.

    <h2>30 Sheets</h2>
              <?php query_posts('cat=' . $thecat . '&tag=30-sheets&order=ASC'); ?>
              <?php if (have_posts()) : ?>
              <?php while (have_posts()) : the_post(); ?>
    
              ---- THE CONTENT YOU WANT TO SHOW GOES HERE ----
    
              <?php endwhile; ?>
    
              <?php else : ?>
              <div id="page">
              <p class="center">There are no records for this media.</p>
              </div>
    
              <?php endif; ?>
    
              <h2>Mall Kiosks</h2>
              <?php query_posts('cat=' . $thecat . '&tag=mall-kiosks&order=ASC'); ?>
              <?php if (have_posts()) : ?>
              <?php while (have_posts()) : the_post(); ?>
    
              ---- THE CONTENT YOU WANT TO SHOW GOES HERE ----
    
              <?php endwhile; ?>
    
              <?php else : ?>
              <div id="page">
              <p class="center">There are no records for this media.</p>
              </div>
    
              <?php endif; ?>

    You can do this for as many loops and tags as you like.

  7. scribu
    Member
    Posted 4 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic