Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In the following query, post_type seems to get ignored resulting in display of all posts from my installation. What have I done wrong?

<?php
function filter_where( $where = '' ) {
    $where .= " AND post_date > '2009-03-01'";
    return $where;
}
add_filter( 'posts_where', 'filter_where' );

$posts = query_posts( '&showposts=-1&post_type=listings' );

remove_filter( 'posts_where', 'filter_where' );

echo '<ol>';
foreach ($posts as $post) {
    echo '<li>' . get_the_title() . '</li>';
}
echo '</ol>';
?>
share|improve this question
    
Pay attention that showposts parameter is Deprecated as of Version 2.1 in favor of 'posts_per_page'. –  Eugene Manuilov Mar 26 '12 at 4:50
1  
oh dear lord, he used query_posts –  Tom J Nowell Jul 24 '12 at 22:58
1  
1  
@TomJNowell's comments make more sense if you watch this awesome (very recent) presentation by core developer Andrew Nacin on doing queries the right way. wordpress.tv/2012/06/15/andrew-nacin-wp_query –  internetross Jul 25 '12 at 1:03
add comment

1 Answer

up vote 1 down vote accepted

Check if your post_type is exactly listings and not listing. Also check this approach:

<?php
function filter_where( $where = '' ) {
    $where .= " AND post_date > '2009-03-01'";
    return $where;
}
add_filter( 'posts_where', 'filter_where' );

function parse_wp_query( $query ) {
    $query->set( 'post_type', array( 'listings' ) );
    return $query;
}
add_filter( 'parse_query', 'parse_wp_query' );

$posts = query_posts( 'posts_per_page=-1&post_type=listings' );

remove_filter( 'posts_where', 'filter_where' );
remove_filter( 'parse_query', 'parse_wp_query' );

echo '<ol>';
foreach ($posts as $post) {
    echo '<li>' . get_the_title() . '</li>';
}
echo '</ol>';
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.