WordPress.org

Forums

[resolved] WP_Query is not displaying posts_from correctly. Is PHP syntax correct? (4 posts)

  1. WPChina
    Member
    Posted 2 years ago #

    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>
  2. David Choi
    Member
    Posted 2 years ago #

    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();

  3. WPChina
    Member
    Posted 2 years ago #

    Excellent, that fixed it placing the function in front :)

  4. David Choi
    Member
    Posted 2 years ago #

    Sure you are welcome!

Topic Closed

This topic has been closed to new replies.

About this Topic