WordPress.org

Ready to get started?Download WordPress

Forums

Post Query by dynamic date (2 posts)

  1. john.shofstall@edn-systems.com
    Member
    Posted 6 months ago #

    I have a client who wants the previous week's posts separated by day (i.e. Yesterday's date followed by a list of posts created yesterday. repeated for 2 days ago, 3 days ago, etc up to 7 days ago.) The code I wrote

    <div class="yesterday">
                <h1 class="day"><?php echo(date("l F d, Y", strtotime('-1 day'))); ?></h1>
                <?php
                function filter_where($where = '') {
                    //posts yesterday
                    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-1 day')) . "'" . " AND post_date < '" . date('Y-m-d') . "'";
                    return $where;
                }
                add_filter('posts_where', 'filter_where');
    
                query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC');
    
                while (have_posts()): the_post(); ?>
                <div class="pastWeek" style="width:100%"><h2><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></h2></div>
                <?php
                endwhile;
                wp_reset_postdata();
                ?>
                </div>

    works on my test site but when I implement onto the live site it starts pulling autosaves, drafts, and revisions, none of which are wanted. In the Codex it says that post_type=any will pull everything EXCEPT revisions, but does not work as it should.
    new.prommanow.com is the test site and of course now as I'm writing that it works it started pulling revisions as well. I've searched for hours and the only way I've found to get the date to be dynamic is the way I've wrote in here. There's a million ways to get static dates like a specific day of the month, but of course there isn't a dynamic way for the day of the week.

  2. john.shofstall@edn-systems.com
    Member
    Posted 6 months ago #

    <?php
    $args = array ( 'post-type' => 'any' , 'date_query' => array( 'after' => '1 day ago' , 'before' => 'today' ) , );
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
      <div class="pastWeek" style="width:100%"><h2><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></h2></div>
    <?php endwhile;
    endif;
    
    // Reset Post Data
    wp_reset_postdata();
    ?>

    seems to be working better, but still not pulling all the posts from that day.

Reply

You must log in to post.

About this Topic

  • RSS feed for this topic
  • Started 6 months ago by john.shofstall@edn-systems.com
  • Latest reply from john.shofstall@edn-systems.com
  • This topic is not resolved
  • WordPress version: 3.8.1