Support » Themes and Templates » display x number of days worth of posts

  • I am trying to write a theme that would be ideal for people who do paid posts, and many of the companies demand that the post be on the front page for a certain number of days. This is a right pain if you have an active blog. Is there a way to code the loop so rather than displaying X number of posts it would rather display x number of days worth of posts regardless of how many there are?

Viewing 13 replies - 1 through 13 (of 13 total)
  • This would place dates upto 10 days ago into an array..

    <?php  $w = 0;
      $i = 1;
      while($w <= 9) {
        $daysago[$i] = strtotime('-'.$i.' day');
        $daysago[$i] = date("Y-m-d", $daysago[$i]);
        echo $daysago[$i].' - '.$i.' days ago<br />'; // Not needed, this just shows each array item with the number of days..

    If you can work out a way to check the post date against the array you should be able to archieve what you want…

    So basically the above makes an array…


    10 times over, with the bottom item being 10 days ago…

    Wp stores post dates with the time in this format…

    yyyy-mm-dd h:i:s

    So you’ll need to grab posts where the date matches in part, any one of the array items…

    in_array() should be able to do it, along with strpos or preg_match….

    Sorry just thinking PHP a little out loud…

    If you don’t know PHP you’ll proberly just be baffled by the above, if not you should have all you need to know to create what you want. There may be a better way, no in fact there proberly is an easier way, but i’ve not had to write something like this so couldn’t say…

    In theory, easy…

    In practice, not so…

    Any real PHP coders here? 🙂

    posts that are so may days old (in this case 46 days)

    $mylimit=46 * 86400; //days * seconds per day
    $post_age = date('U') - get_post_time('U');
    if ($post_age < $mylimit) {
    echo 'this post is within my date limit ';

    Austin Matzko threw this little gem out to the wp-hackers email list on Feb 28, 2009:
    If you want to query for a tag with ID of 3 in the last 5 days, do the following:

    function filter_where($w = '') {
            $w .= " AND post_date > '" . date('Y-m-d', strtotime('-5 days')) . "'";
            return $w;
    add_filter('posts_where', 'filter_where');
            'tag__in' => array(3),

    I knew they’d be something simple to handle it….

    What’s the usage of the above exactly..?

    This is brilliant. I am glad this second post was made as I don’t know php well enough to implement the first version easily.

    $mylimit=46 * 86400; //days * seconds per day
    $post_age = date(‘U’) – get_post_time(‘U’);
    if ($post_age < $mylimit) {
    echo ‘this post is within my date limit ‘;

    What would be the argument to restrict this by category?

    If I was using that with the WordPress Default theme on main page, I would add this line just before <?php if (have_posts()) : ?> in wp-content/themes/default/index.php:

    query_posts($query_string . '&cat=4') //get cat 4 posts

    t31os said “What’s the usage of the above exactly..? “

    Not sure what you are asking…you could put that code in your index.php right before your loop.

    It’s ok, just wanted to be clear on whether it could be plonked straight into a page and where…

    You’ve covered that now… 😉

    I’m just a little confused because i had trouble passing variables into a wordpress query….

    I could print or echo a variable value onto the page, as soon as it was used inside a query (using new WP_Query and wp_reset_query where necessary) it would always end up being using the value…

    Example lets say i had query_posts(‘cat=’.$mycat.”);

    And mycat was set to 5…

    I found it still returned posts for other categories… not just those in cat 5…

    Perhaps it was me in error, but i’m sure i double checked and triple checked..

    Basically i was building a “featured posts” function for fun and trying to use variables for passing into the query. The code is gone now, but i’ll be at it again soon… i’m constantly re-writing and chopping code up, but i did find that behaviour odd…

    I also tried using an array’ed variables to build parts of the query, again it seemed to just grab all posts as if it was ignoring my variables.

    Sorry to hijack a little, i think the OP is answered anyhow and figured i’d sneak in an odd question… 😉 *cheeky.. 🙂

    This should work:

    $mycat = '5';

    and this as well:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    t31os not hijacking this at all i am still playing around with the idea and any commentary on this subject brings me further understanding of my problem.

    Cool, thanx..

    What i had been trying to do was create an easy way to loop through 3,4 or 5 categories, 5 posts from each using an array to store the categories i want to pull from, then making however many queries i need…. without writing the 3,4 or 5 queries…

    I’ve managed to use this without a problem…

    $mylim = 2;
    $cat_array = array(
    $dstyle1 = '
    $dstyle2 = '
      border:1px solid #000;
    $i = 1;
    <div style="<?php echo $dstyle1; ?>">
    foreach($cat_array as $value) { ?>
      <div style="<?php echo $dstyle2; ?>">
      <?php echo ucfirst($value); ?>
      <br /><br />
      <?php $myqa{$i} = new WP_Query('category_name='.$value.'&showposts='.$mylim.'');?>
      <?php while ($myqa{$i}->have_posts()) : $myqa{$i}->the_post(); ?>
      <a href="<?php the_permalink() ?>" rel="bookmark" title="Perma Link to <?php the_title_attribute(); ?>">
      <?php the_title();  ?>

    Excuse the inline styling etc… (it’s quicker while still testing/playing)… Loops through each array item and grabs the set amount of posts…

    My normal loop starts after that…

    Of course i’m yet to toy with Michael’s examples above…

    The foreach thing had me going in circles for hours… 🙂

    When I put this code:

    $mylimit=14 * 86400; //days * seconds per day
    $post_age = date('U') - get_post_time('U');
    if ($post_age < $mylimit) {
    echo 'this post is within my date limit ';

    before my loop if statement, it gives me exactly what I want, which is posts from the last X days, in this case 14. But when I click the “next” button to go to the next page, it comes up empty. Is there a way for the next button to get the next 14 days worth of posts? I’d be great if there was a plugin to do this, where we could set an option like the one in the admin interface to say “display X posts/days”.

    I’m looking to show a BUY button on my site only on the most recent post (and the homepage, because that will be the most recent post anyways. I’m only showing one post at a time).

    For posts that have been expired for 24 hours, the buy button needs to be hidden.

    I think this is possible with the above, but I can’t get it to work.

    could someone please help? Thank you so much!! I’m desperate since I have to launch this very soon! Thanks.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘display x number of days worth of posts’ is closed to new replies.