Yeah, something went wrong with the paragraph right under the code, but you get the meaning, right?
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: http://weblogtoolscollection.com/archives/2008/05/17/how-to-avoid-duplicate-posts/
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.
Alex
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):
<?php
$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:
<?php
$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!):
<?php
$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();
?>
...