• Resolved sgardner

    (@sgardner)


    If i add the code directly above the Loop will this display posts only from the last 14 days?

    <?php query_posts(‘day= -14’); ?>

    If not how can i achieve this. I have tried but cant get it to work as expected.

    Thanks

    Steven

Viewing 12 replies - 1 through 12 (of 12 total)
  • No…but the Time parameters example explains one solution.

    Thread Starter sgardner

    (@sgardner)

    Yes this is where i read first but i dont understand how to impliment posts only within the last 14 days.

    <?php
      function filter_where($where = '') {
        //posts in the last 14 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-14 days')) . "'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    ?>
    Thread Starter sgardner

    (@sgardner)

    Ok I re-read the page you linked to and i overlooked what was plain to see. but now i have another problem.

    this is the code;

    <?php
    //based on Austin Matzko's code from wp-hackers email list
      function filter_where($where = '') {
        //posts in the last 30 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-14 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-16'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    ?>

    when i limit the posts to display the last 14 days this also effects the “recent posts” widget. I have the widget to display the last 5 posts but the list stops after the 14 day cut off.

    How can i have the time limit on the Homepage Main Section Only, not effecting the widgets and other pages.

    Thanks
    Steven

    Try remove_filter('posts_where', 'filter_where'); after the query_posts.

    I’m trying to use this function to display a COUNT of all posts published in the last 30 days, but not actually display the posts.

    any hints on getting this done?

    [moderated–bump removed. Please refrain from bumping as per Forum Rules]

    <?php
      function filter_where($where = '') {
        //posts in the last 30 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
        $args=array(
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'caller_get_posts'=> 1
          );
    $myposts=new WP_Query($args);
    remove_filter('posts_where', 'filter_where');
    echo 'posts in last 30 days '.count($myposts);
    wp_reset_query(); //just in case
    ?>

    Thank you!

    the function is returning 1 as the count however, even though there are many more. Any idea why?

    Ah, need to use count($myposts->posts). Fixed below with a few changes, posts for last 90 days, with a ‘verification’ loop.

    <?php
      function filter_where($where = '') {
        //posts in the last 30 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-90 days')) . "'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
        $args=array(
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'caller_get_posts'=> 1
          );
    $my_query=new WP_Query($args);
    remove_filter('posts_where', 'filter_where');
    
      if( $my_query->have_posts() ) {
        echo '<h2>Number of posts in last 90 days is '.count($my_query->posts) . '</h2>';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><small><?php the_time('m.d.y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
         <?php
        endwhile;
      } //if ($my_query)
    wp_reset_query(); //just in case
    ?>

    oh my god… you have SOLVED IT!

    I owe you big time.

    I know I’m just milking it, but I also need to run this function twice on the same page. My ultimate goal is to have 2 separate counts of all new posts from 2 separate categories.

    What do I need to change to do that?

    bump

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Will this code limit the posts displayed to the last 14 days?’ is closed to new replies.