Forums

how to wp_query for both future and past posts? (3 posts)

  1. mikewillnot
    Member
    Posted 6 months ago #

    I'm modifying a theme with a "featured" panel that shows a specified number of (n) posts. By default, it shows the (n) most recent. I have the options set so that n=4. I'd like to modify the wp_query statement so that it returns the two most recent plus the two next future posts, i.e. the present moment +/- 2 posts. I guess that it might be necessary to have two loops, one future and one recent, but I'm wondering if it's possible to construct a combined statement that does the trick.

    Here's the original query statement, followed by my modified version that returns only FUTURE posts.

    ORIGINAL

    <?php $recent = new WP_Query("cat=".$feature_cat_1."&amp;showposts=".$feature_cat_1_num); while($recent->have_posts()) : $recent->the_post();?>
    				<?php if( get_post_meta($post->ID, "thumbnail", true) ): ?>
    				    <a href="<?php the_permalink() ?>" rel="bookmark"><img style="float:left;margin:0px 10px 0px 0px;" src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="<?php the_title(); ?>" /></a>
    				<?php else: ?>
    				<?php endif; ?>
    				<b><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_time('l, F j, Y'); ?> - <?php the_title(); ?></a></b>
    				<?php the_content(__('[Read more]'));?>

    MODIFIED FOR FUTURE

    <?php $recent = new WP_Query('category_name=contras&amp;post_status=future&amp;order=ASC'); while($recent->have_posts()) : $recent->the_post();?>
    				<?php if( get_post_meta($post->ID, "thumbnail", true) ): ?>
    				    <a href="<?php the_permalink() ?>" rel="bookmark"><img style="float:left;margin:0px 10px 0px 0px;" src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="<?php the_title(); ?>" /></a>
    				<?php else: ?>
    				<?php endif; ?>
    <b><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_time('l, F j, Y'); ?> - <?php the_title(); ?></a></b>
    				<?php the_content(__('[Read more]'));?>
  2. MichaelH
    moderator
    Posted 6 months ago #

    Either need two loops as you've described, or put the post IDs for both future/past posts into an array

    Something like this where $post_ids is an array of those post IDS:

    query_posts(array('post__in' => $post_ids));

  3. mikewillnot
    Member
    Posted 3 weeks ago #

    I'm still (or, again) working on this problem, only now instead of past, it's just current, for today that needs to appear above future. I now have this query

    <?php $recent = new WP_Query('showposts=1&category_name=english&order=DESC'); while($recent->have_posts()) : $recent->the_post();?>

    The above code produces the post for the current day, but I only get the correct results if there is only one post for today. If there are two or more today (and I want to return them all) I need to query specifically for the current date. I can't figure out how to do that.

    The code above is a modified version of what I got with my theme; I'm really a php newbie.

    Any help would be greatly appreciated. THANKS.

Reply

You must log in to post.

About this Topic

Tags