WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display published posts after 30 minutes passed (2 posts)

  1. JamesOakley
    Member
    Posted 5 years ago #

    I'm trying to find a way to make wordpress display posts after 30 minutes from their publishing time. So if the post is published now, it will be displayed after 30 minutes not immediately. I can't use future date for posts because each post must retain its actual time. So if a post is published at 8:00 AM, it should be displayed at 8:30 AM but retains it's actual time which is 8:00 AM.

    Some categories must be delayed 30 minutes while others must be delayed 60 minutes. So is there a way to modify the WP_Query in order to list posts within a specific time period?

    Is there a way to accomplish this with WordPress?

  2. JamesOakley
    Member
    Posted 5 years ago #

    I was able to accomplish this through two steps:
    1- Modify wordpress query.php to support adding a new parameter to the query string.
    2- Use php to calculate the new delayed time.

    Step 1:

    Open wp-includes/query.php and search for this line:

    if ( $q['w'] )
    			$where .= " AND WEEK($wpdb->posts.post_date, 1)='" . $q['w'] . "'";

    and replace it with:

    if ( $q['w'] )
    			$where .= " AND WEEK($wpdb->posts.post_date, 1)='" . $q['w'] . "'";
    
    		if ( $q['before'] )
    			$where .= " AND $wpdb->posts.post_date < '" . $q['before'] . "'";

    Step 2:

    Here is my php function strtotime (check http://php.net/strtotime) to calculate the 2 hours delay time:
    $unix_delayed_time = date(strtotime("-120 minutes"));

    then I convert the unix timestamp generated by strtotime into a normal Mysql date format using:
    $delayed_time = date("Y-n-j G:i:s", $unix_delayed_time);

    then I used it in a loop:

    query_posts('cat=3&before=$delayed_time');
    
    // the Loop
    while (have_posts()) : the_post();
      // the content of the post
      the_content('Read the full post »');
    endwhile;

    So the total code is:

    <?php
    $unix_delayed_time = date(strtotime("-120 minutes"));
    $delayed_time = date("Y-n-j G:i:s", $unix_delayed_time);
    query_posts('cat=3&before=$delayed_time');
    
    // the Loop
    while (have_posts()) : the_post();
      // the content of the post
      the_content('Read the full post »');
    endwhile;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic