Forums

Adding time of a month to the query_posts ? (4 posts)

  1. flippy146
    Member
    Posted 1 year ago #

    <?php query_posts( 'category_name=av' );
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    Seen above is my current used code, showing one category using the query. Now my question is, how can I display the posts of one month?

    The alternate solution is to show the posts of the current month, but I want to go a little further. I want it to show the posts of the last 30 days, is this possible. If so, please tell me how! =]

    Thanks in advance,
    Flip

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

  3. flippy146
    Member
    Posted 1 year ago #

    Oke so I have `// Create a new filtering function that will add our where clause to the query
    function filter_where( $where = '' ) {
    // posts for March 1 to March 15, 2010
    $where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'";
    return $where;
    }
    add_filter( 'posts_where', 'filter_where' );

    $query = new WP_Query( $query_string );`

    I am not really an experienced filter user, should I add this function to my functions.php? And then how do I integrate it in my current query_posts?

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    to show posts of the last 30 days:
    add this to functions.php of your theme:

    // Create a new filtering function that will add our where clause to the query
    function filter_where( $where = '' ) {
    	// posts in the last 30 days
    	$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    	return $where;
    }

    then add this before your loop:

    add_filter( 'posts_where', 'filter_where' );
    
    query_posts( $query_string . '&category_name=av');

    to avoid having the 30-day-limit in all queries,
    after your loop, add:

    remove_filter( 'posts_where', 'filter_where' );

Topic Closed

This topic has been closed to new replies.

About this Topic