• Resolved WPChina

    (@wordpresschina)


    On WP_Query I am trying to figure out how to code correctly to show the past 45 days of Posts from categories 7,8,12,34. I am trying to use “posts_where” too but I can not figure out the syntax. Here is what I am incorrectly doing:

    <ul>
        <?php
    		$recentPosts = new WP_Query();
    		$recentPosts->query('cat=7,8,12,34');
    		while ($recentPosts->have_posts()) :
    		$recentPosts->the_post();
    function filter_where( $where = '' ) {
    	// posts in the last 45 days
    	$where .= " AND post_date > '" . date('Y-m-d', strtotime('-45 days')) . "'";
    	return $where;
    }
    add_filter( 'posts_where', 'filter_where' );
    $query = new WP_Query( $query_string );
    remove_filter( 'posts_where', 'filter_where' );
    	?>
    
        <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    
    	<?php endwhile; ?>
    	</ul>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Try moving the codes

    function filter_where( $where = ” ) {
    // posts in the last 45 days
    $where .= ” AND post_date > ‘” . date(‘Y-m-d’, strtotime(‘-45 days’)) . “‘”;
    return $where;
    }
    add_filter( ‘posts_where’, ‘filter_where’ );

    right above

    $recentPosts = new WP_Query();

    Thread Starter WPChina

    (@wordpresschina)

    Excellent, that fixed it placing the function in front 🙂

    Sure you are welcome!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP_Query is not displaying posts_from correctly. Is PHP syntax correct?’ is closed to new replies.