WordPress.org

Ready to get started?Download WordPress

Forums

Show the posts published before a specific date (7 posts)

  1. dilan86
    Member
    Posted 4 years ago #

    Hi to all!

    I would a query_posts that shows the posts with a date before a X date, is there a way to do it?

    I just tryed to do "monthnum<X" but it's wrong :(

    Help me please...

  2. MichaelH
    Member
    Posted 4 years ago #

    Based on Austin Matzko's (filosofo) post to wp-hackers list

    Tested this using the WordPress Default theme's index.php just before start of the loop (<?php if (have_posts()) : ?>):

    <?php
      function filter_where($where = '') {
        //posts in the last 30 days
        //$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
        //posts  30 to 60 days old
        //$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
        //posts for March 1 to March 15, 2009
        $where .= " AND post_date >= '2009-03-01' AND post_date <= '2009-03-15'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    ?>

    Left a few extra lines with // to provide examples of variations of date ranges that might be useful.

  3. 1800collect
    Member
    Posted 4 years ago #

    Is there anyway to make this only effect one loop on a page? I have multiple loops and it seems to be effecting them all.

  4. 1800collect
    Member
    Posted 4 years ago #

    function filter_where($where = '') {
        $where .= " AND post_date >= '" . date('Y-m-d') . "'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    
    // CAN EFFECT THIS LOOP
    
    query_posts('cat=1&showposts=1&order=ASC');
    while (have_posts()) : the_post();
      the_excerpt();
    endwhile;
    
    // I don't want it to effect this loop.
    query_posts('cat=3&showposts=4&order=ASC');
    while (have_posts()) : the_post();
          the_content();
    endwhile;

    It effects the second loop, which I don't want. What am I doing wrong?

    Also you may wonder why I am doing this since it would be much easier to just get the most recent thing. We'll we display future dates as published dates because it an event calendar. I just want to display the nearest upcoming event. So I don't want that July 2 event to be shown nor do I want that May 30 event that has already past be shown.

  5. 1800collect
    Member
    Posted 4 years ago #

    Can anyone point me in the right direction?

    I found this thread: http://wordpress.org/support/topic/280001?replies=2

    But wp_reset_query() doesn't seem to work.

    These guys seemed to not get it to work either:

    http://wordpress.org/support/topic/199070?replies=2

    Here is the entire code. It is a custom page.

    <?php // POST THE PAGE CONTENT
    
    		if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div class="post" id="post-<?php the_ID(); ?>">
    		<h2 class="hide"><?php the_title(); ?></h2>
    			<div class="entry">
    				<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    
    				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
    			</div>
    		</div>
    		<?php endwhile; endif;  //END PAGE CONTENT ?>
    
    		<?php 
    
    			function filter_where($where = '') {
    				$where .= " AND post_date >= '" . date('Y-m-d') . "'";
    				return $where;
    			}
    			add_filter('posts_where', 'filter_where');
    			query_posts($query_string . 'cat=1&showposts=5&order=ASC');
    
    			if (have_posts()) : while (have_posts()) : the_post();  // THE LOOP
    				the_excerpt();
    			endwhile; endif;	
    
    			wp_reset_query(); //Should reset the query so it doesn't effect the one below, but it does not work.
    
    			query_posts('cat=3&showposts=1'); //Should not be effected by original query since it should have been reset
    
    			if (have_posts()) : while (have_posts()) : the_post();
    				echo '<div style="background:#ccc;">';
    				the_excerpt();
    				echo '</div>';
    			endwhile; endif;
    		?>
  6. prouton
    Member
    Posted 4 years ago #

    I realize that I'm responding two months after the last post, but I'll offer this info for anyone else searching for how to solve this. Mind you, I haven't yet performed this, but the answer seems relatively straightforward: Since the date limitation is being done with a filter, not in the "query_posts" parameter, the filter needs to be removed after the first loop and before the second loop starts.

    Why would "wp_reset_query" remove a properly added filter? It shouldn't, that's all.

  7. boldock
    Member
    Posted 4 years ago #

    I think you need to put this after the enwhile of your first loop:

    <?php remove_filter('posts_where', 'filter_where'); ?>

    i search that a long time today and finally it works fine for me .

Topic Closed

This topic has been closed to new replies.

About this Topic