• jezthomp

    (@jezthomp)


    Is there any way of querying the posts to display only the current months posts, say only Mays.

    Then in an archive page show all the archive months excluding the current one your on, say excluding May but show, April,March,Feb, Jan etc etc..

    Anyway to do this pref coding but a plugin perhaps?

    Many thanks 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • vtxyzzy

    (@vtxyzzy)

    You can get the current month’s posts by using the monthnum parameter:

    $monthnum = date('n')
    query_posts("monthnum=$monthnum");

    Excluding the current month from the archive list can be done like this:

    <?php
    $archives = wp_get_archives('echo=0');
    $arcarray = explode('<li>',$archives);
    $junk = array_shift($arcarray); // First entry is empty
    $junk = array_shift($arcarray); // Current month is next
    $result = '<li>' . implode('<li>',$arcarray);
    echo $result;
    ?>
    Thread Starter jezthomp

    (@jezthomp)

    Thanks for that vt.

    So for example i wanted 2 pages, one showing the posts from the current month then another page showing all other months it would work like this…

    Also only from cat id 1…

    Current months posts…

    <?php if (have_posts()) : ?>
    
    <?php query_posts('cat=1');?>
    <?php
    $monthnum = date('n')
    query_posts("monthnum=$monthnum");
    ?>
    		<?php while (have_posts()) : the_post(); ?>
    
    			<!--News content current month-->
    
    			<?php the_content() ?>
    
    		<?php endwhile; ?>

    Surely the following is only listing an archive list, any chance it could control the posts?

    All other months posts….

    <?php if (have_posts()) : ?>
    
    <?php query_posts('cat=1');?>
    <?php
    $archives = wp_get_archives('echo=0');
    $arcarray = explode('<li>',$archives);
    $junk = array_shift($arcarray); // First entry is empty
    $junk = array_shift($arcarray); // Current month is next
    $result = '<li>' . implode('<li>',$arcarray);
    echo $result;
    ?>
    		<?php while (have_posts()) : the_post(); ?>
    
    			<?php the_content() ?>
    
    <?php endwhile; ?>

    Thanks again 🙂

    vtxyzzy

    (@vtxyzzy)

    I misunderstood what you wanted for the second part. You are right, what I showed only gives an archive list.

    Then in an archive page show all the archive months excluding the current one

    It is more complicated to retrieve all posts, especially since you will probably want pagination. I will look into that more, but it may be more complex than I can get into.

    As for the first part, I think this is what you want:

    <?php
    $monthnum = date('n')
    query_posts("monthnum=$monthnum&cat=1");
    if (have_posts()) :
       while (have_posts()) : the_post(); ?>
          <!--News content current month-->
          <?php the_content() ?>
       <?php endwhile; ?>
       <!-- Pagination here -->
    <?php else: ?>
       <!-- No posts found here -->
    <?php endif; ?>

    ‘if (have_posts())’ always goes after query_posts(), and each query_posts overrides the previous one.

    vtxyzzy

    (@vtxyzzy)

    To get posts in prior months, use the code below for your query_posts:

    //based on Austin Matzko's code from wp-hackers email list
      function filter_where($where = '') {
        //posts from prior months
        $year = date(Y);
        $monthnum = date(m);
        $where .= " AND post_date < '$year-$monthnum-01'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
    $ignore_stickies = 1;  // Stickies will mess up the order
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("caller_get_posts=$ignore_stickies&paged=$paged");
    Thread Starter jezthomp

    (@jezthomp)

    Thanks vt, i’ll try them out.

    Very much appreciated 🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Posts: Excluding/Including Current Month?’ is closed to new replies.