Actually, it’s not. I’ve used that exact piece of code several times, and it works just fine.
Why don’t you tell us *why* you think it’s wrong?
It’s totally possible that I’m not understanding it. But the Codex says you need to turn the var $do_not_duplicate into an array. But I don’t think it’s doing that.
The instructions also tell you to change this line twice:
<?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
That can’t be right. Why would I change it twice? Once should be enough.
So when I’m finished, the original code snippet looks like this, if I follow the instructions in the Codex:
<?php $my_query = new WP_Query('category_name=featured&showposts=2');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
<!-- Do stuff... -->
<?php endwhile; ?>
<!-- Do other stuff... -->
<?php if (have_posts()) : while (have_posts()) : the_post(); if($post->ID == $do_not_duplicate[0] || $post->ID == $do_not_duplicate[1] || $post->ID == $do_not_duplicate[2]) continue; update_post_caches($posts); ?>
<!-- Do stuff... -->
<?php endwhile; endif; ?>
That looks wrong, and in fact doesn’t work.
Am I missing something? I’m a little shaky on arrays.
The Codex is def. wrong.
The end result should be:
<?php $my_query = new WP_Query('category_name=featured&showposts=2');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID ?>
<!-- Do stuff... -->
<?php endwhile; ?>
<!-- Do other stuff... -->
<?php if (have_posts()) : while (have_posts()) : the_post(); if($post->ID == $do_not_duplicate[0] || $post->ID == $do_not_duplicate[1] || $post->ID == $do_not_duplicate[2]) continue; update_post_caches($posts); ?>
<!-- Do stuff... -->
<?php endwhile; endif; ?>
and in the instructions, the first step should go like this:
replace
<?php $my_query = new WP_Query('category_name=featured&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
with
<?php $my_query = new WP_Query('category_name=featured&showposts=2');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID ?>
doodlebee if you used that exact code several times, you better go check your blogs again, as that code is busted.
You’re right, Bonusball.
So edit the Codex. It’s a wiki. Sign up for an account and edit away.
Personally, I would use
if ( in_array( $do_not_duplicate, $post->ID) ) continue;
instead of all of those ors ( || )…
There really is an issue with this kind of code, though. If you have your options set to display 5 posts and you ‘continue’ past 3 of them in your main loop (because they’re in the $do_not_duplicate array), you’ll only see two posts – because the query doesn’t reset.
Well, what do you know, I didn’t know it was a wiki.
Although if I’m logged in as Bonusball, I shouldn’t need to make another username for the wiki!
Regardless, I fixed it. and I used your suggestion stevejohnson, although I fixed your code, too. It should have been
if (in_array($post->ID, $do_not_duplicate)) continue;
And I now what you’re saying about the problem, but that doesn’t happen.
Who knows why?
I’ve done my duty, off to dinner!
I’m new to php, so the syntax is still tricky to me. I can’t seem to get this to work. Here’s my code:
<?php query_posts('category_name=featured');
if ( have_posts() ) : while ( have_posts() ) : the_post();
$do_not_duplicate = $post->ID;
$custom_field_date = get_post_meta($post->ID, 'project-date', true);
$custom_field_disciplines = get_post_meta($post->ID, 'disciplines', true);?>
<!--do stuff-->
<?php endwhile;
wp_reset_query();
?>
<?php query_posts('category_name=winners');
while ( have_posts() ) : the_post();
$do_not_duplicate = $post->ID;
$custom_field_date = get_post_meta($post->ID, 'project-date', true);
$custom_field_disciplines = get_post_meta($post->ID, 'disciplines', true);?>
<!--do stuff-->
<?php endwhile;
wp_reset_query();
?>
<?php query_posts('category_name=portfolio&showposts=50');
while ( have_posts() ) : the_post();
$do_not_duplicate = $post->ID;
$custom_field_date = get_post_meta($post->ID, 'project-date', true);
$custom_field_disciplines = get_post_meta($post->ID, 'disciplines', true);?>
<!--do stuff-->
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
What am I doing wrong?