0

I want to retrieve inside my archive-CPT.php most recent upcoming post ( post with status of future) and past post (oldest published post).

This is how my archive-CPT.php is looking like:

 <?php

get_header(); ?>

<div id="primary" class="row">

    <?php if($layout == 'left_side'){ ?>
    <aside id="side-bar" class="span4">
            <?php dynamic_sidebar( 'sidebar_left'); ?>
    </aside>
    <?php } ?>  
    <?php if($layout == 'both_side'){ ?>
    <aside id="side-bar" class="span3">
            <?php dynamic_sidebar( 'sidebar_left' ); ?>
    </aside>
    <?php } ?>  

    <div id="content" class="margin span9" role="main">
    <?php 

        if( have_posts() ){ 
            // normal blog layout
                $x = 1;
                while ( have_posts() ){
                    the_post(); 
                    if ( 0 === (int) $post->post_parent ) {
                    get_template_part( 'inc/post-format/content-debate');
                }

                    $x++;
                }
            }

        else{ ?>
        <article class="type-page box">
            <h1 class="title"><?php _e('Post not found', 'outbox'); ?></h1>
            <div class="the-content">
            <p class="lead"><?php _e('We could not find that post you were looking for.', 'outbox'); ?></p>
            <br>
            <h3><?php _e('Try searching', 'outbox') ?></h3>
            <?php echo get_search_form(); ?>
            <?php get_template_part( 'inc/recent-posts' ); ?>               
            </div>
        </article>
        <?php } 

        kriesi_pagination(); 

        ?> 
        <div class="debatesinfo">
        <div class="row-fluid">
            <div class="span6">
            <h3>UPCOMING DEBATES</h3>
            <?php
            $query = new WP_Query( array(
             'post_status' => 'future',
             'orderby' => 'date', 
             'order' => 'ASC', 
             'posts_per_page' => 1 
           ) );

         if( $query->have_posts() ) {
         while( $query->have_posts() ) {
         $query->the_post();
         echo the_title();
    } 
  }     
  ?>
    </div>
    </div><!-- #content -->
    <?php if($layout == 'right_side'){ ?>
    <aside id="side-bar" class="span4">
            <?php dynamic_sidebar( 'sidebar_right' ); ?>
    </aside>
    <?php } ?>      
    <?php if($layout == 'both_side'){ ?>
    <aside id="side-bar" class="span3">
            <?php dynamic_sidebar( 'sidebar_right' ); ?>
    </aside>
    <?php } ?>      
</div><!-- #primary -->

and this code is from the functions.php which is for retrieving the most recent published post for the main loop :

function wpse124228_alter_ppp_order_for_mycpt( $query ) {
if ( ! $query->is_main_query() || is_admin() )
    return;
if ( is_post_type_archive( 'debate' ) ) {
    //Only display 1 post on mycpt archive
    $query->set( 'posts_per_page', 1 );
    //Most recent/current
    $query->set( 'orderby', 'date' );
    $query->set( 'order', 'DESC' );
 }
  }
add_action( 'pre_get_posts', 'wpse124228_alter_ppp_order_for_mycpt' );

Any suggestions on how can I retrieve the upcoming post and the last post titles and thumbnail images ?

Eric Holmes Edit: fixed a typo in the code.

2
  • 2
    See this answer for ways to break a single loop into multiple parts.
    – Milo
    Nov 21, 2013 at 18:28
  • I've looked through your answer but I don't understand how exactly I can retrieve what I need. I've updated my question with more information and relevant code. Maybe you can help me...
    – agis
    Nov 24, 2013 at 21:04

1 Answer 1

2

For Future Posts, you can just have Published Posts that are scheduled for the future. When editing a post, you can click on Edit beside "Publish Immediately" and select a date/time.

You can then have a subloop get all posts with the post_status of future. That should work just fine for you!

WP_Query documentation

Future:

$query = new WP_Query( array(
    'post_status' => 'future',
    'orderby' => 'date', 
    'order' => 'ASC', 
    'posts_per_page' => 1 
) );

if( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();
        echo the_title();
    }
}

Oldest

$query = new WP_Query( array(
    'orderby' => 'date', 
    'order' => 'ASC', 
    'posts_per_page' => 1 
) );

if( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();
        echo the_title();
    }
}
6
  • I've updated my question with more relevant info and code, can you help retrieving the upcoming post and the last post ?
    – agis
    Nov 24, 2013 at 21:09
  • 1
    Added. look at WP_Query documentation if I missed anything. Nov 25, 2013 at 14:05
  • I've tried but it seems that I don't put the code correctly because I'm getting errors. I've updated my question with how I've added the code. I'm sure that I'm adding it wrong. Can you help me with this I'm a PHP beginner so I don't really know how exactly I should embed the code.
    – agis
    Nov 25, 2013 at 16:16
  • Nope, my mistake -the 'post_status' => 'future' had an incorrect closing quote - ` instead of '. My mistake! Fixed in my answer and your question. Nov 25, 2013 at 16:25
  • I've added the code but it doesn't retrieve it, it should retrieve the post title of the post with the status of future no ? Or maybe I should add also the 'post_type' => 'myCPT' in oreder to work?
    – agis
    Nov 25, 2013 at 16:35

Your Answer

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

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