WordPress.org

Forums

[resolved] Will this code limit the posts displayed to the last 14 days? (13 posts)

  1. s.gardner
    Member
    Posted 5 years ago #

    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

  2. MichaelH
    Member
    Posted 5 years ago #

    No...but the Time parameters example explains one solution.

  3. s.gardner
    Member
    Posted 5 years ago #

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

  4. MichaelH
    Member
    Posted 5 years ago #

    <?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);
    ?>
  5. s.gardner
    Member
    Posted 5 years ago #

    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

  6. MichaelH
    Member
    Posted 5 years ago #

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

  7. fcarthy
    Member
    Posted 5 years ago #

    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]

  8. MichaelH
    Member
    Posted 5 years ago #

    <?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
    ?>
  9. fcarthy
    Member
    Posted 5 years ago #

    Thank you!

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

  10. MichaelH
    Member
    Posted 5 years ago #

    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
    ?>
  11. fcarthy
    Member
    Posted 5 years ago #

    oh my god... you have SOLVED IT!

    I owe you big time.

  12. fcarthy
    Member
    Posted 5 years ago #

    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?

  13. fcarthy
    Member
    Posted 5 years ago #

    bump

Topic Closed

This topic has been closed to new replies.

About this Topic