PHP Help with WordPress Page Template

Status
Not open for further replies.

waikikicane

Registered Member
Joined
Jul 5, 2011
Messages
87
Reaction score
45
After an extenisive search I finally found the following code that lets me display my WP posts on a WP page. This is great, but what I really need is to be able to display just the posts created in the last 24 hours. Does anyone know what changes I need to make to limit the results to just the last 24 hours or so?

Code:
<?php/*
Template Name: All posts
*/
?>
<?php
$debut = 0; //The first article to be displayed
?>
<?php while(have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<ul>
<?php
$myposts = get_posts('numberposts=-1&offset=$debut');
foreach($myposts as $post) :
?>
<li><?php the_time('d/m/y') ?>: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

<?php endwhile; ?>


Please tell me how it would cost me to have this coded adapted to my need.

Thanks!

 
You can go to the wordpress codex to reference how to build custom query loops, search for wp_query and go to the codex wordpress org class reference page. That will show you how to use a custom loop once you have created it. I have built the custom query below for finding Posts that were published in the last 24 hours (server time btw):

Code:
<?php
#Get yesterday 24 hours ago
$yesterday = time() - (24 * 60 * 60);
$ydate = getdate($yesterday);

$the_query = new WP_Query(array(
    'date_query' => array(
        'after' => array(
            'year' => $ydate['year'],
            'month' => $ydate['mon'],
            'day' => $ydate['mday'],
            'hour' => $ydate['hours']        )
    )
));
#I copied and pasted this next part directly from wordpress codex, you can customize what data you want displayed from each post below
if ( $the_query->have_posts() ) : ?>

    <!-- pagination here -->

    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
 
Last edited:
Status
Not open for further replies.
This thread has been auto closed due to the forum's thread age policy. Read more.
Back
Top