Showing posts between a certain date range
Asked Answered
E

5

4

Trying to display my custom post types for specific date ranges. I want to show posts only within a certain month. I know I need to hook into the posts_where filter, but I can not figure out how to pass arguments to this function, as I need to pass the date range.

I have seen plenty of examples of how to alter the WHERE clause to take a date range, but only when static. I need to do the following:

add_filter('posts_where', 'my_custom_where', '', '04/2011'); //pass my date to the filter

function my_custom_where( $where = '' ) {

    //figure range  
    $range = array(
        'start' => $date . '-1',
        'end' => $date . '-' . cal_days_in_month(CAL_GREGORIAN, date('m', $date), date('Y', $date))
    );

    $where .= " AND post_date ....."; //build where with date range

    return $where;

}

Hope that makes sense. Any help would be appreciated.

Execrate answered 26/4, 2011 at 4:15 Comment(0)
T
7

You can make it dynamic if you resort to global variables.(Not ideal, but hey, I haven't found a cleaner way...)

First define a global variable for the date

$GLOBALS['start_date'] = '2011-07-31';

then add your filter

add_filter( 'posts_where', 'filter_where' );

 function filter_where( $date, $where = '' ) {
    // posts later than dynamic date
    $where .= " AND post_date >= '" . date('Y-m-d H:i:s', strtotime($GLOBALS['start_date'])) . "'";
    return $where;
}

Then remove the filter if you just want to run that for a single query.

Tosch answered 7/9, 2011 at 13:30 Comment(0)
D
3

This should do the trick to grab posts in the last 30 days. Beware, however, that this code, placed in your functions.php file, or in a plugin will filter your posts EVERYWHERE. If you only want it to filter on some pages, either wrap it in conditional tags, or use it on a template page:

<?php
  function filter_where($where = '') {
    // Posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
  }
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

I stole this code directly from: http://wordpress.org/support/topic/show-the-posts-published-before-a-specific-date?replies=2#post-1066144, where there is a bigger discussion of this issue and there are more examples if this doesn't get exactly what you wanted.

Dalenedalenna answered 26/4, 2011 at 5:3 Comment(1)
Yes, I have seen this code. I am not trying to display posts for the last 30 days. I am trying to display posts for a 30 day period, which is dynamic, and chosen by the user from a drop down box. Very similar to a post archive, but used for a custom display. So I need to pass this dynamic date range to the filter.Execrate
Y
1
<?php
// Show post from a theme option where posts_month is the month 1-12 and posts_year is the year (4 dig)

$month = get_option( 'posts_month' );
$year = get_option( 'posts_year' );

$query = new WP_Query( 'year=' . $year . '&monthnum=' . $month );
Yetah answered 26/4, 2011 at 9:52 Comment(0)
V
0

If you not want to do any coding work then it is possible with the help WordPress Posts listing plugin .With help of this you can Display Posts between a certain date range.

Just provide Start and End Date. You can display today, current week, last week, this month and last month posts. You can display posts published in last N Days, if the predefined options are not suitable for you

Valma answered 11/8, 2016 at 9:15 Comment(0)
N
0

This is a very old question, but I think it can be useful for others. This is the method that gives me the best result as the simple way to understand and can replace the numbers. Also if you want to show only up to a date, you can remove the array "after" to show from the "before" date and the oldest posts.

This way with the parse_query filter I get this to work for all widgets, and other custom areas with WP_query or get_posts calls.

function my_home_by_period($query) {
    $query->set('date_query', array(
        array(
            'after'  => array(
                'year'   => 2021,
                'month'  => 10,
                'day'    => 1,
            ),
            'before'     => array(
                'year'   => 2021,
                'month'  => 10,
                'day'    => 31,
            ),
            'inclusive'  => true,
        )
    ));
}
add_action('parse_query', 'my_home_by_period');
Nyctophobia answered 23/5, 2022 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.