0

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>';
?>
1

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>';
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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