1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Help with WordPress Page Template

Discussion in 'Hire a Freelancer' started by waikikicane, Mar 16, 2015.

  1. waikikicane

    waikikicane Registered Member

    Joined:
    Jul 5, 2011
    Messages:
    81
    Likes Received:
    42
    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!

     
  2. ralphnsk

    ralphnsk Newbie

    Joined:
    Nov 2, 2009
    Messages:
    39
    Likes Received:
    13
    Location:
    /dev/random
    I've PMed you the solution ;)
     
    • Thanks Thanks x 1
  3. waikikicane

    waikikicane Registered Member

    Joined:
    Jul 5, 2011
    Messages:
    81
    Likes Received:
    42
    Thank you thank you thank you!
     
  4. vayacondios

    vayacondios Newbie

    Joined:
    Mar 3, 2015
    Messages:
    30
    Likes Received:
    5
    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: Mar 16, 2015