WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Strange WP_Query behavior (3 posts)

  1. puvira
    Member
    Posted 4 months ago #

    Hi,

    I am experiencing very strange results using WP_Query. I want to display all posts in my left menu (custom theme). For the record, all the posts belong under category 1 or 4. My code is as follows, located in a file included as a template part leftmenu.php

    wp_reset_query();
    $args = array(
        'post_type' => 'post',
        'orderby' => 'ID',
        'order' => 'ASC'
        )
    $my_query = new WP_Query($args)

    Problem: In the resulting query, few posts from category 4 are missing.

    If I modify the query as follows:

    wp_reset_query();
    $args = array(
        'post_type' => 'post',
        'category__in' => array(4),
        'orderby' => 'ID',
        'order' => 'ASC'
        )
    $my_query = new WP_Query($args)

    suddenly, the query is correctly returning all the previously hidden posts from category 4.

    Let´s modify the query once again, to include both category 1 and 4:

    wp_reset_query();
    $args = array(
        'post_type' => 'post',
        'category__in' => array(1,4),
        'orderby' => 'ID',
        'order' => 'ASC'
        )
    $my_query = new WP_Query($args)

    and the result is the same as with the first query, some posts from category 4 are missing! I var_dumped $my_query to check.

    Any hint what could be the possible reason for such a behavior? Am I missing something? If the first query doesn´t return some posts based on the post_type criteria, how can the second query return these posts when the criteria is even more strict (filtering based on category added)? And why the first query doesn´t return some posts?

    Any help or hint is greatly appreciated, I already spent hours thinking this problem over and over and studying the Codex.

  2. alchymyth
    Forum Moderator
    Posted 4 months ago #

    how many posts are showing?
    is anything limiting that number?

    try to add 'posts_per_page' => -1 to the query to show unlimited posts.

    what theme are you using?

    does that also happen with the default theme Twenty Fourteen and with all plugins deactivated?

  3. puvira
    Member
    Posted 4 months ago #

    'posts_per_page' did the trick!
    you´re a genius, alchymyth.

    there is so much to learn..
    many thanks!

Reply

You must log in to post.

About this Topic

Tags