• Hi folks,

    Not sure if this wants to go here, or in hacks, but I’ll happily delete and move if you like.

    I’m currently trying to develop a blog theme that has a bit more of a conversational style, and one of the things that I would like to do is have home.php display all the posts from the last day on which there were any posts (e.g., all of today’s posts if there are any, and if not, all of those from the first day back on which there were posts).

    I’m aware that this is probably going to be a custom query of the format query_posts('day=$parameter') before the loop, but I’m not sure what the parameter wants to be, or how to define it. I’m currently in that awkward stage of knowing how to mess around with some of the php, but not knowing all of the relevant code. Even if you can only offer pointers to resources concerning the passing of time and post count variables in WP, the help would be much appreciated.

    Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • I hit on this previously for handling a ‘latest month’ display:

    http://wordpress.org/support/topic/43758

    Something like the following should work for the latest day’s worth of posts:

    <?php
    $latest = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 1");
    $latest = str_replace('-', '', substr($latest, 0, 10));
    // echo $latest;
    query_posts('m=' . $latest . '&showposts=-1');
    $wp_query->is_archive = false; $wp_query->is_home = true;
    ?>
    Thread Starter icememory

    (@icememory)

    Thanks for that, I’ll give it a whirl!

    Bump. I want posts on a given day but I also want a forward / back button on the bottom. How will I do that?
    Thanks.

    Here is the index.php I really don’t know how to fix this with the wp_query stuff which I don’t fully understand. Sorry about the messy file but thats how I got the pages to work…

    <?php
    /* Template Name: Main Index
    */
    ?>
    <?php
    get_header();
    ?>
    
    <?php
    if (is_home()) {
    $latest = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 1");
    $latest = str_replace('-', '', substr($latest, 0, 10));
    //echo $latest;
    query_posts('m=' . $latest . '&showposts=-1');
    $wp_query->is_archive = false; $wp_query->is_home = true;
    } else { $post = $wp_query->post; }?>
    
    <?php if (in_category('12') && is_home()==false) { ?>
    <div id="comic">
    <?php } else { ?>
    <div id="content">
    <?php } ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?php if (in_category('12') && is_home()==false) { ?>
      <div class="entry_comic">
    <h3 class="etitle" id="post-<?php the_ID(); ?>"> <a href="<?php the_permalink() ?>" rel="bookmark">
          <?php the_title(); ?>
          </a> </h3>
    <div align="center">
    <a href="http://66.7.201.40/~cujpybvo/comic/2007/07/11/sanchit-is-always-messin-with-mimes/"><img src="<?php echo bloginfo('template_directory');?>/images/first.gif" border="0"></a>
    <?php
    $wp_query->is_single = true;
    previous_cat_post($beforeGroup='', $afterGroup='', $beforeEach='', $afterEach='', $showtitle=false, $textForEach='<img src="../../../../../wp-content/themes/mushblue-10/images/back.gif" border="0">');
    $wp_query->is_single = false;
    ?>
    &nbsp;<a href="<?php echo bloginfo('url');?>/"><img src="<?php echo bloginfo('template_directory');?>/images/home.gif" border="0"></a>
    <?php
    $wp_query->is_single = true;
    next_cat_post($beforeGroup='', $afterGroup='', $beforeEach='', $afterEach='', $showtitle=false, $textForEach='<img src="../../../../../wp-content/themes/mushblue-10/images/next.gif" border="0">');
    $wp_query->is_single = false;
    ?>
    &nbsp;<a href="http://66.7.201.40/~cujpybvo/comic/"><img src="<?php echo bloginfo('template_directory');?>/images/new.gif" border="0"></a>
    </div>
    <?php } else { ?>
      <div class="entry">
    <div class="date"><p><span class="month"><?php the_time('M') ?></span><span class="day"> <?php the_time('j') ?> </span></p></div>
    <h3 class="etitle" id="post-<?php the_ID(); ?>"> <a href="<?php the_permalink() ?>" rel="bookmark">
          <?php the_title(); ?>
          </a> </h3>
             <div class="emeta">
                  Posted by:&nbsp;<?php the_author_posts_link(); ?><br>
                  <?php //the_time('F dS, Y');
                        echo "  Category: "; the_category(',');
                  ?>
              </div>
    
    <?php } ?>
    
        <div class="ebody">
    <?php if (!empty($post->post_excerpt) && is_home()) { the_excerpt(); } else {the_content(__('Read more')); }?>
    <?php if (in_category('12')) { ?>
    <?php edit_post_link(__('<strong>Edit</strong>'));?>
    <?php } else { ?>
       <?php comments_popup_link( 'No comments ','1 comment ','% comments ','clink ',''); edit_post_link(__('<strong>Edit</strong>'));?>
    <?php } ?>
        </div>
        <!-- <?php trackback_rdf(); ?> -->
    <?php if (!in_category('12')) { ?>
      <?php comments_template(); // Get wp-comments.php template ?>
    <?php } ?>
      </div>
      <?php endwhile; else: ?>
      <p>
        <?php _e('Sorry, no posts matched your criteria.'); ?>
      </p>
      <?php endif; ?>
    <?php if (in_category('12') && is_home()==false) { ?>
    <div align="center">
    <a href="http://66.7.201.40/~cujpybvo/comic/2007/07/11/sanchit-is-always-messin-with-mimes/"><img src="<?php echo bloginfo('template_directory');?>/images/first.gif" border="0"></a>
    <?php
    $wp_query->is_single = true;
    previous_cat_post($beforeGroup='', $afterGroup='', $beforeEach='', $afterEach='', $showtitle=false, $textForEach='<img src="../../../../../wp-content/themes/mushblue-10/images/back.gif" border="0">');
    $wp_query->is_single = false;
    ?>
    &nbsp;<a href="<?php echo bloginfo('url');?>/"><img src="<?php echo bloginfo('template_directory');?>/images/home.gif" border="0"></a>
    <?php
    $wp_query->is_single = true;
    next_cat_post($beforeGroup='', $afterGroup='', $beforeEach='', $afterEach='', $showtitle=false, $textForEach='<img src="../../../../../wp-content/themes/mushblue-10/images/next.gif" border="0">');
    $wp_query->is_single = false;
    ?>
    &nbsp;<a href="http://66.7.201.40/~cujpybvo/comic/"><img src="<?php echo bloginfo('template_directory');?>/images/new.gif" border="0"></a>
    </div>
    </div>
    <?php } else { ?>
      <p>
        <?php posts_nav_link(' — ', __('&laquo; Previous Page'), __('Next Page &raquo;')); ?>
      </p>
      </div>
    
    <?php get_sidebar(); ?>
    <?php } ?>
    <!-- The main column ends  -->
    <?php get_footer(); ?>

    Bump. I’m curious, too. This worked perfectly on my blog to show only the latest day’s posts on the front page, but I lost my “Previous Entry” link. Any suggestions, please? (I have don’t have the code installed right now, because I don’t want to lose that link, but what it would be if I did have it installed follows. Thanks!)

    <?php get_header(); ?>
    
    	<div id="content">
    
    <?php
    $latest = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 1");
    $latest = str_replace('-', '', substr($latest, 0, 10));
    // echo $latest;
    query_posts('m=' . $latest . '&showposts=-1');
    $wp_query->is_archive = false; $wp_query->is_home = true;
    ?>
    <!-- put your loop here -->
    
    	<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    			<div class="post" id="post-<?php the_ID(); ?>">
    <?php the_time('l, j') ?> <?php the_time('F Y') ?><br>
    <br>
    
    <p class="deck"><?php the_nice_category(" , ", " and "); ?></p>
    				<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    
    				<div class="entry">
    					<?php the_content('Read the rest of this entry &raquo;'); ?>
    				</div>
    
    				<p class="postmetadata">
    					<?php the_time() ?> | <?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
    				</p>
    			</div>
    
    		<?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Previous Entry') ?></div>
    			<div class="alignright"><?php previous_posts_link('Next Entry &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2>Not Found</h2>
    		<p>Sorry, but you are looking for something that isn't here.</p>
    		<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    	<?php endif; ?>
    
    	</div>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Regaining the “Previous Entry” link may not be hard, but what exactly do you expect to find after following the link?

    If you merely want to paginate through all of the last day’s posts (assuming there’s a lot of them), it would help to know how many posts per page are set (or expected) to figure out for example an offset to pass query_posts().

    If instead you’re looking to page through each day’s worth of posts… well, you’re asking for a few fundamental changes in how WordPress behaves, or at least a good deal of code to work around the built-in functionality.

    Thank you for the reply! I guess I was hoping there was an easy way to only display that latest day’s posts on the front page, and that, yes, the Previous Entry link would take me to the posts for the day before that, and so on and so on. (My idea was that my blog could be kind of link a daily magazine, where it was easy to browse backwards from the home page.) I gather from your reply, however, that it wouldn’t be at all simple to accomplish that.

    Is that because I’d (potentially) have a different number of posts each day, and WordPress relies upon that quantity (rather than another argument, like “same day”) to figure out what to display on the Previous Entry pages?

    Thanks!

    “kind of like a daily magazine,” that is.

    Is that because I’d (potentially) have a different number of posts each day, and WordPress relies upon that quantity (rather than another argument, like “same day”) to figure out what to display on the Previous Entry pages?

    Not quite. The basic problem here is one of history. By that I mean for a proper query to occur on subsequent pages, some knowledge of what’s come before — and potentially, after — is in order.

    In the case of a default query with (next/previous) pagination, where posts fall in regards to the date is taken care of through the query’s original sort. But in the end it’s of no actual concern here, since WordPress relies on a simple formula of how many posts should appear on a page, and on what page one is presently on, to calculate which posts appear on any particular page, as well as provide the to and fro links.

    Now *some* of the functionality being asked for could be slipped into the pre-existing archive query structure by simply passing the archive date-based vars to query_posts() along with ‘showposts=-1’. But next/previous also fails here because all posts for the present query would already be displayed. For this a whole new function would have to be written which checks for posts on days ‘next’ and ‘previous’ to the currently displayed day, and then link up if they exist.

    Paging through posts day to day right off the home page requires even more because a simple “page 2” tells us nothing about what ‘day’ we are viewing, nor how that relates to possible days before and after. It’s not an impossible task, but it would take a lot more than a few tweaks or couple lines of PHP.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘display all posts from latest day with posts’ is closed to new replies.