WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] List Posts with Certain Tag by Month/Year (14 posts)

  1. Matt Banks
    Member
    Posted 4 years ago #

    I'm not great with PHP, so I'm having some trouble. I'm trying to pull all posts with a certain tag, display them in unordered lists broken out by month/year in descending order. So, for example:

    December 2009

    • Posts with certain tag from this month

    November 2009

    • Posts with certain tag from this month

    etc...

    I've been reading stuff in the Codex but it's over my head. I've been able to get one big unordered list with everything from the tag, but I don't know how to break it down by month/year.

    Can anyone help?

    Thanks!
    - Matt

  2. MichaelH
    Member
    Posted 4 years ago #

    <?php
    // for tag1, get all posts in descending date order, on change in Month Year print heading
        $args=array(
          'tag'     => 'tag1',
          'posts_per_page' => -1,
          'orderby' => 'date',
          'order' => 'DESC',
          'caller_get_posts'=>1
        );
    
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          $ymdate = '';
           while ($my_query->have_posts()) : $my_query->the_post();
             $ympost = mysql2date("M Y", $post->post_date);
             if ( $ympost != $ymdate) {
               $ymdate = $ympost;
               echo '<h2>Posts for ' . $ymdate . '</h2>';
             }
             ?>
            <p><small><?php the_time('F jS, Y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
            //the_content('Read the rest of this entry &raquo;');
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    
    ?>
  3. Matt Banks
    Member
    Posted 4 years ago #

    Ok, I kinda got it, and here's the code I'm using (copied from various sources plus things of my own)...

    <?php
    	$months = $wpdb->get_results("SELECT DISTINCT MONTH(post_date) AS month , YEAR(post_date) AS year
    		FROM $wpdb->posts
    		WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month, year ORDER BY post_date DESC");
    
    	$posts = $wpdb->get_results("SELECT id, post_title, MONTH(post_date) AS month , YEAR(post_date) AS year
    		FROM $wpdb->posts wposts, $wpdb->terms wterms, $wpdb->term_relationships wterm_relationships, $wpdb->term_taxonomy wterm_taxonomy
    		WHERE wterm_relationships.object_id = wposts.ID
    		AND wterm_relationships.term_taxonomy_id = wterm_taxonomy.term_taxonomy_id
    		AND wterms.term_id = wterm_taxonomy.term_id
    		AND wterm_taxonomy.taxonomy = 'post_tag'
    		AND wterms.name = 'Reviews'
    		AND wposts.post_status = 'publish'
    		AND wposts.post_type = 'post'
    		ORDER BY post_date DESC");
    
      foreach($months as $this_month){ ?>
    
    	<p><?php echo date("F", mktime(0, 0, 0, $this_month->month, 1, $this_month->year)); ?> <?php echo $this_month->year; ?></p>
    	<ul>
    	<?php for ($i = 0; $i <= count($posts); $i++){?>
    	  <?php if(($this_month->year == $posts[$i]->year)&&($this_month->month == $posts[$i]->month)){?>
    	  <li><a href="<?php echo get_permalink($posts[$i]->id); ?>"><?php echo $posts[$i]->post_title; ?></a></li>
    	  <?php } ?>
    	<?php } ?>
    	</ul>
      <?php } ?>

    It's showing up properly, however I'd like it to NOT show month names if there are no posts that month, which I can't seem to figure out.

    Any ideas?

  4. Matt Banks
    Member
    Posted 4 years ago #

    Thanks MichaelH!

    Is there any way to make that an unordered list instead of paragraphs for each item? I tried it myself but I'm unsure where to put the closing ul tag?

    Also, is it possible to display the full month name instead of the abbreviation?

  5. MichaelH
    Member
    Posted 4 years ago #

    Full month name

    $ympost = mysql2date("F Y", $post->post_date);

    Formatting_Date_and_Time

    <ul> after the while
    </ul> after the endwhile
  6. Matt Banks
    Member
    Posted 4 years ago #

    If I put the closing UL tag after the endwhile, it only closes the list after the last month shown, not after each month.

    <?php
    // for tag1, get all posts in descending date order, on change in Month Year print heading
    	$args=array(
    	  'tag'     => 'tag1',
    	  'posts_per_page' => -1,
    	  'orderby' => 'date',
    	  'order' => 'DESC',
    	  'caller_get_posts'=>1
    	);
    
    	$my_query = new WP_Query($args);
    	if( $my_query->have_posts() ) {
    	  $ymdate = '';
    	   while ($my_query->have_posts()) : $my_query->the_post();
    		 $ympost = mysql2date("F Y", $post->post_date);
    		 if ( $ympost != $ymdate) {
    		   $ymdate = $ympost;
    		   echo '<p>'.$ymdate.'</p>';
    		   echo '<ul>';
    		 }
    		 ?>
    		<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    	   <?php
    		//the_content('Read the rest of this entry &raquo;');
    	  endwhile;
    	  echo '</ul>';
    	} //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  7. MichaelH
    Member
    Posted 4 years ago #

    So you'd want it after?
    $ymdate = $ympost;

  8. Matt Banks
    Member
    Posted 4 years ago #

    If I could give you a hug, I would. :)

    THANK YOU SO SO SO MUCH!!!!

  9. Matt Banks
    Member
    Posted 4 years ago #

    Actually, I have on last question. Will this script still update properly if I am using WP Super Cache?

  10. MichaelH
    Member
    Posted 4 years ago #

    Don't know. Guess you'd have to try it.

  11. emila3
    Member
    Posted 4 years ago #

    Hi. This is exactly what I need & works great.
    Can I use it with a category instead of a tag?

    I have tried substituting:

    'tag' => 'tag1'

    with:

    'category' => 'category1' -- or -- 'cat' => 'category1'

    Neither seems to work. Am I missing something?

    Thanks in advance!

  12. Ryan L
    Member
    Posted 3 years ago #

    Hey Emila, for categories you have to specify by ID, or if you want it by name you have to use "category_name". For example:

    'category_name' => 'name_of_your_category',
    'cat' => '1',
    'category__and' => array(1,2,3),

    MichaelH, I appreciate all this code that you have shared. However, I have a question as well. I want to limit the number of posts displayed (I am using this for a gallery), but whenever I try and build a next or prev button they don't work. I am using default code just to test:

    <div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>

    Thanks.

  13. Ryan L
    Member
    Posted 3 years ago #

    Ah, I guess even if I did get it to work, it wouldn't matter? It would just reset this loop and show the same posts on each page. Is it possible to make this work in conjunction with separate pages?

  14. tomconte
    Member
    Posted 3 years ago #

    What's the proper way of excluding the current month from display? I have it set up like this:

    <? foreach(array_slice($months,1) as $this_month) : ?>

    Not sure if I will experience problems down the road due to the array becoming larger over time. Any suggestions? I'm curious to see if there is a better way.

Topic Closed

This topic has been closed to new replies.

About this Topic