WordPress.org

Ready to get started?Download WordPress

Forums

Something seems wrong with WP_Query, how to debug? (13 posts)

  1. marsev
    Member
    Posted 2 years ago #

    Hi there,

    I have a custom loop inside a "single"-template for a custom post type:

    $posts_list = new WP_Query( array ( 'post_type'      => 'post',
    	'posts_per_page' => -1,
    	 'meta_key'       => 'customFieldXY',
              'meta_value'     => 'value' ) );
    if ( $posts_list->have_posts() ) {
        while ( $posts_list->have_posts() ) : $posts_list->the_post();
            echo get_the_title();
        endwhile;
    }

    Besides the expected results there are also some posts showing up, that do not match these criterias. I also tried something simplier like 'post__in' => array( 2, 5, 12, 14, 20 ) and got the same error with the same strange posts showing up. What is wrong with the query? Is there any possibility to debug and get a better understanding what the query does and what goes wrong?

    Thanks
    Marcel

  2. esmi
    Forum Moderator
    Posted 2 years ago #

  3. marsev
    Member
    Posted 2 years ago #

    Yeah thanks, I know that reference and use it quite often. But it does not help me solving this specific problem. All methods of wp_query indicate that there are more posts returned than it should be the case. And I do not see any error in my query (especially in the very simple one).

    I assume that some special side effect is playing a major role in the error. But how to check that?

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    What exactly are you trying to do with that query?

  5. marsev
    Member
    Posted 2 years ago #

    I am looking for posts where a custom field has a specific value (a given post_id). In most cases the custom field is not set.

    After trying with more simplier queries I assume the problem has nothing to do with my specific query. Querying a specific post works just fine ("p") but querying an array of posts ("post__in") already fails (posts are shown that are not in that list).

  6. esmi
    Forum Moderator
    Posted 2 years ago #

    I've never has these problems with WP_Query. Have you reset any previous queries?

  7. marsev
    Member
    Posted 2 years ago #

    I reduced the complexity of the query to the minimum. The following query returns me a total of 21 posts although it should be only one. As you can see I did a reset for the query first.

    wp_reset_query();
    $posts_list = new WP_Query( array ( 'post_type'      => 'post',
                                        'posts_per_page' => -1,
                                        'post__in'       => array ( 1723 ) ) );
    if ( $posts_list->have_posts() ) {
        echo $posts_list->post_count;
    }
  8. esmi
    Forum Moderator
    Posted 2 years ago #

    Try setting posts_per_page to 1.

  9. marsev
    Member
    Posted 2 years ago #

    Still 21 posts :-/

  10. esmi
    Forum Moderator
    Posted 2 years ago #

    Whatever this is, it appears to be specific to your site. I've never come across this with WP_Query before.

  11. marsev
    Member
    Posted 2 years ago #

    It gets worse: The query fails everywhere, even when no other query was loaded before (plain php-page which includes wp-config.php) ???

  12. marsev
    Member
    Posted 2 years ago #

    I got it :-): the shown posts were all sticky posts. Now with 'post__not_in' => get_option( 'sticky_posts' ) everything works fine.

    However ... this is not very "intuitive" :-/.

  13. esmi
    Forum Moderator
    Posted 2 years ago #

    Gosh - you must have a lot of sticky posts!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags