Support » Developing with WordPress » WP_Query problem (this must be possible…)

  • Hi,

    I have a problem on a homepage on which I want to display a loop calling the the last 4 News (cat=20).

    I have a Featured panel, where a post from the Blog can be dispayed or one from the News category, using a custom field to select the featured item.

    The problem is that if a post form the News is selected as featured, in the News panel I get 3 news instead of 4 (showposts=4).

    Featured panel query:

    $featured = new WP_Query('meta_key=featured&meta_value=on');
    while($featured -> have_posts()) : $featured -> the_post();
    $do_not_duplicate = $post->ID;

    News panel query:

    $news = new WP_Query('cat=20&showposts=4');
    while($news -> have_posts()) : $news -> the_post();
    if ($post->ID == $do_not_duplicate) continue;

    Why if there are two different query, the News query still cound the fact that in the featured query there is a post from that category and puts only 3 posts and not 4?

    How can I work it out so that if I select a post from the News category as featured, the News panel still displays 4 news, not 3?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Yeah, something went wrong with the paragraph right under the code, but you get the meaning, right?

    Moderator Ipstenu (Mika Epstein)


    Lead Plugin Wrangler

    They’re two different loops. If you want to exclude anything that’s in featured from the news loop, you’ll need to Exclude posts from some category.

    Ipstenu, I am afraid I wasn’t clear enough.

    I have a Featured panel and a News panel, on the same page (homepage).

    The News loop is set to display 4 last news (shoposts=4).

    In the admin I can select a post to be the Featured article (with custom fields).

    Now, if I select the a post that is in the News as “featured” I get 3 News in the panel and not 4 anymore. Like if the News loop counts 3 posts + the one in the Futured query.

    I know how to exclude posts from a category. It just doesn’t help me here.

    The category (News) may be the same for the two loops. So I can’t exclude it, right?

    If I select a post from the Blog category as featured, of course, I get 4 News in the News panel. If instead I select a post from the News category as featured, then the News panel displays 3 news. The News query count the News in the Featured panel, even if in a different query.

    That is not logical to me. Or am I missing something?

    I can’t believe nobody can figure this out.

    Gian-ava, its unfortunately a little more complicated than that, I fell into the same trap.

    Check out this tutorial:

    That should help you. Also, have a look at post__not_in. I’ve not used it, so can’t really say what it does, but that might help to.


    Edit: The reason what you have no isn’t working, is because the loop is getting 4 posts, then excluding them if they are in the array of featured posts. In your case it does this:
    Get me 4 posts in News;
    Are any of these posts in Featured;
    If yes exclude them;
    Output 3 posts in News that aren’t in Featured.

    What we want it to do it is the other way round!

    Anyone found a way to do this? I’m also in the same situation, although I’m using a function to determine whether a post in my query has a particular custom_field. Everything works, except the query runs through the posts, then outputs the number of posts dictated in showposts minus the number of posts excluded by the custom_field function.

    … So it’s somewhat broken on my end too.

    Roelani try this. Let’s fix the same initial example (Note the brackets on $do_no_duplicate):

    $featured = new WP_Query('meta_key=featured&meta_value=on');
    while($featured -> have_posts()) : $featured -> the_post();
    $do_not_duplicate[] = $post->ID;

    The second loop will use this variable in the “posts_not_in” parameter:

    $other = new WP_Query(array(
    'post__not_in' => $do_not_duplicate
    while($other -> have_posts()) : $other -> the_post();
    $do_not_duplicate_other[] = $post->ID;

    Suppose you have a third loop which do not show posts from the first and second loops. Note the variable $do_not_show (without brackets!):

    $do_not_show = array_merge($do_not_duplicate , $do_not_duplicate_other);
    $third = new WP_Query(array(
    'post__not_in' => $do_not_show
    while($third -> have_posts()) : $third -> the_post();
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WP_Query problem (this must be possible…)’ is closed to new replies.