• [email protected]

    (@johnshofstalledn-systemscom)


    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.

Viewing 1 replies (of 1 total)
  • Thread Starter [email protected]

    (@johnshofstalledn-systemscom)

    <?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.

Viewing 1 replies (of 1 total)
  • The topic ‘Post Query by dynamic date’ is closed to new replies.