How to display Section for certain time

Check the time parameter on WP_Query Class. There is a example on how to show posts form last 30 days. You can do it for last couple of hours, minutes or even seconds! Code is tested and working.

$args = array(
                            'posts_per_page' => 1, // We are showing only one post
                            'category_name' => 'prime' // showing form category prime
                            );


add_filter( 'posts_where', 'filter_where' );
$the_query = new WP_Query( $args );
remove_filter( 'posts_where', 'filter_where' ); ?>

 <div id="prime_news" class="col-full">
 <?php
    if ($the_query->have_posts()) :?>
<div id="prime_headline">
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php endwhile; ?>
<div class="prime_left fl">
<h3><a href="https://wordpress.stackexchange.com/questions/58897/<?php the_permalink(); ?>" title="" class="title_slide"><?php the_title(); ?></a></h3>
<p class="prime_excerpt"><?php echo limit_words(get_the_excerpt(), '190'); ?><span  class="read-more"><a href="https://wordpress.stackexchange.com/questions/58897/<?php the_permalink(); ?>"><?php _e('&#2310;&#2327;&#2375;  &#2346;&#2338;&#2375;...', 'BharatNews'); ?></a></span></p></div>
<div class="prime_right fr"><?php bharti_get_image('image',560,320,' '.$GLOBALS['align']); ?></div>
</div><div class="clear"></div> 
    <?php endif; ?>

 </div> 

Add this function on functions.php

function filter_where( $where="" ) {
    // posts published last 30 minutes
    $where .= " AND post_date > '" . date('Y-m-d H:i:s', strtotime('-30 minutes')) . "'";
    return $where;
}

Leave a Comment