3

I would like to know if anyone knows a way of adding a timeframe (say 20 years) to the datestamp on a set of posts, and then running a query on the loop which uses the new date?

Basically what I want to do is have a series of normal news posts. In addition to that, I have a custom post type for '20 years ago today', with the posts in that date stamped appropriately in the early 90s. I'd like for those posts to appear in the same loop as the news posts (rather than in a separate loop).

So it might go:

  • News Post (21 March 2012)
  • News Post (19 March 2012)
  • 20 years ago (17 March 1992)
  • News Post (12 March 2012)
  • 20 years ago (6 March 1992)

and so on. And anything that happened less than 20 years ago (say, in 1993), would behave like a pending post, and not show up.

Any suggestions would be appreciated

1
  • really interesting question. wish i had the answer, but it looks like kaiser's got you covered. Mar 22, 2012 at 11:42

1 Answer 1

2

An example, that uses the posts_where filter. If you need to extend a query using the posts_clauses filter, then just exchange $where with $pieces and set $pieces['where'] .= instead of $where .=. Just drop that into your functions.php file and add some conditional tag before querying the posts.

function filter_where( $where ) 
{
    // Add some conditionals and abort if they're not met:
    // @example Abort on pages
    if ( ! is_page() )
        return;

    // 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 between 01.03.2012 and 21.03.2012
    $where .= " AND post_date >= '2012-03-01' AND post_date <= '2012-03-21'";

    return $where;
}
add_filter( 'posts_where', 'filter_where' );

Edit: So, here's the original loop from the OP:

<section id="content">
<?php 
query_posts( array( 'post_type' => array( 'post', 'twentyyearsago' ) ) ); 
if ( have_posts() )
{
while ( have_posts() )
{
    the_post();

    if ( 'twentyyearsago' === get_post_type() ) // FIX THIS: strict type checking with "==="
    {
    ?>
        <article class="twentyyears">
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="This day 20 years ago">This day 20 years ago</a></h2>
            <?php the_content(); ?>
            <footer><?php the_time( 'F j, Y' );?></footer> // FIX THIS: One leading "<" in front of "<?php" too much
        </article>
    <?php } else { ?>
        <article class="post">
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            <?php the_content(); ?>
            <footer><?php the_time( 'F j, Y' ); ?></footer>
        </article>

    <?php 
    } // FIX THIS: one trailing ";" too much
} // endwhile;
else
{
    // do stuff
    echo "No Posts.";
} // endif; 
wp_reset_query(); 
?>
</section>
9
  • Hi Kaiser, Thanks for your reply. I'm a little confused, as I'm not familiar with posts_where or posts_clauses. I've dropped the function into my functions.php file, and can see it filtering down the date. That's not quite what I'm after. What I'm trying to achieve is to add 20years to the date of any post with the custom post type "twentyyearsago", before it's pressed by query_posts (I've included my query bellow, showing them together in the same loop) <?php query_posts( array( 'post_type' => array( 'post', 'twentyyearsago' ) ) ); ?>
    – Nathan
    Mar 21, 2012 at 14:18
  • Both are simply filters. The first offers the WHERE part as string to modify, the later offers all parts of the query to modify.
    – kaiser
    Mar 21, 2012 at 14:23
  • Just question the post type on the top of the function (see comment about conditionals) to skip queries that are not related to your custom post type. Then add the dates you inside the filter (the last part with the dates).
    – kaiser
    Mar 21, 2012 at 14:24
  • I did find this bit of php to modify the date after the face (which if I can't get this to work, provides a workaround if I change all the past dates to the present ie. 1992 to 2012, and then use this so they at least display as 1992 on the page, but it's not ideal) Could it be used in some way to modify the time before they're queried by query_posts? $date = date_create(get_the_time('F j, Y')); date_add($date, date_interval_create_from_date_string('+20 years')); echo date_format($date, 'F j, Y');
    – Nathan
    Mar 21, 2012 at 14:26
  • 1
    Oh, not blaming your answer at all, I realise it's a lack of understanding on my part. I've been through the conditional tags on the codex, and the php operators, and I've tried all manner of combinations, and to be honest, I'm completely at a loss. If it's of any help, here's my loop and my function. And to my eyes, I don't see anything in that function that is supposed to add the 20years (or any time frame) to the date - it just filters to a date range.
    – Nathan
    Mar 22, 2012 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.