WordPress.org

Ready to get started?Download WordPress

Forums

display x number of days worth of posts (14 posts)

  1. bradstinyworld
    Member
    Posted 5 years ago #

    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?

  2. t31os
    Member
    Posted 5 years ago #

    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]);
        $w++;
        echo $daysago[$i].' - '.$i.' days ago<br />'; // Not needed, this just shows each array item with the number of days..
        $i++;
      }?>

    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...

    year-month-day

    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? :-)

  3. MichaelH
    Member
    Posted 5 years ago #

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

    <?php
    $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');
    
    query_posts(array(
            'tag__in' => array(3),
    ));
  4. t31os
    Member
    Posted 5 years ago #

    I knew they'd be something simple to handle it....

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

  5. bradstinyworld
    Member
    Posted 5 years ago #

    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.

  6. bradstinyworld
    Member
    Posted 5 years ago #

    <?php
    $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?

  7. MichaelH
    Member
    Posted 5 years ago #

    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
  8. MichaelH
    Member
    Posted 5 years ago #

    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.

  9. t31os
    Member
    Posted 5 years ago #

    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.. :)

  10. MichaelH
    Member
    Posted 5 years ago #

    This should work:

    $mycat = '5';
    query_posts('cat='.$mycat);

    and this as well:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       $args=array(
       'showposts'=>4,
       'cat__in'=>array('4'),
       'paged'=>$paged,
       'caller_get_posts'=>1
       );
    query_posts($args);
  11. bradstinyworld
    Member
    Posted 5 years ago #

    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.

  12. t31os
    Member
    Posted 5 years ago #

    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(
    'catname1',
    'catname2',
    'catname3'
    );
    
    $dstyle1 = '
      margin:0;
      width:auto;
      position:relative;
      top:0;
      left:173px;
      display:block;
      float:left;
      border:none;
      padding:5px';
    
    $dstyle2 = '
      margin:5px;
      width:140px;
      overflow:hidden;
      position:relative;
      float:left;
      background:#dee1d8;
      color:black;
      top:0;
      left:0;
      border:1px solid #000;
      padding:6px';
    $i = 1;
    ?>
    <div style="<?php echo $dstyle1; ?>">
    
    <?php
    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.'');?>
      <ul>
      <?php while ($myqa{$i}->have_posts()) : $myqa{$i}->the_post(); ?>
      <li>
      <a href="<?php the_permalink() ?>" rel="bookmark" title="Perma Link to <?php the_title_attribute(); ?>">
      <?php the_title();  ?>
      </a>
      </li>
      <?php
      endwhile;
      ?>
      </ul>
      </div>
      <?php
      $i++;
      wp_reset_query();
    }
    ?>
    </div>

    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... :-)

  13. jeff_n
    Member
    Posted 5 years ago #

    When I put this code:

    <?php
    $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".

  14. tjmapes
    Member
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags