Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am literally clueless as to why the code does not function properly on the first page, but functions as expected on the second page. I have already tried running the second query via another WP_Query() to no avail. See for yourself: http://blog.donovanglover.com/

The second query should return the latest 3 posts in the pagination loop, yet it also returns posts from the first query. Instead of 3 posts on the homepage, there are 6, but on the second page, you see the expected 3.

My code is below:

<section id="main" class="c6" role="main">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    <header id="important-stuff">
        <h2 id="featured">Featured Posts</h2>
        <div class="slider">
            <div class="slides">
<?php wp_reset_query(); ?>
<?php $sticky_query = new WP_Query('nopaging=true'); ?>
<?php if(have_posts()) : while ($sticky_query->have_posts()) : $sticky_query->the_post(); ?>
<?php if(is_sticky()) : ?>
                <div class="slide"><a rel="bookmark" href="<?php the_permalink(); ?>"><?php echo the_post_thumbnail('full'); ?><h2><?php the_title(); ?></h2></a></div>
<?php endif; ?>
<?php endwhile; else: ?>
                <p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
                <div class="left-arrow"></div>
                <div class="right-arrow"></div>
            </div>
        </div>
    </header>
    <footer id="new-stuff">
        <h3>The New Stuff</h3>
            <ul>
<?php wp_reset_query(); ?>
<?php query_posts('showposts=3&paged='.$paged); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
                <div class="clearfix"><li>
                    <h4><a rel="bookmark" href="<?php the_permalink(); ?>">
                        <?php echo the_post_thumbnail('full'); ?>
                        <span><?php the_title(); ?></span>
                    </a></h4>
                    <?php the_excerpt(); ?>
                </li></div>
<?php endwhile; else: ?>
                <p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
            </ul>
            <h4 class="page-nav clearfix">
                <span class="next-page"><?php previous_posts_link('&larr; Newer Entries') ?></span>
                <span class="prev-page"><?php next_posts_link('Older Entries &rarr;') ?></span>
            </h4>
    </footer>
</section>

Am I doing something wrong, or would this be the expected output?

share|improve this question
1  
I can't look at your page and see what you expect to be happening, especially on a busy page like that. Please elaborate. –  s_ha_dum Mar 26 '13 at 14:06
    
@s_ha_dum Ah, my bad. I've updated the post to reflect the specifics. –  railgun Mar 26 '13 at 14:12
2  
the second loop outputs the sticky posts, as any default loop would do on the index page. imho, you will need to exclude the sticky posts from the second loop, in whihc case you should consider to code this using WP_Query(). –  Michael Mar 26 '13 at 14:39

2 Answers 2

up vote 6 down vote accepted

I see a number of issues, aside from the use of query_posts ( this should raise immediate alarm bells ). I strongly recommend you don't use the alt syntax, and that you minimise the unnecessary opening and closing PHP tags as it obscures your code.

Here is a cleaned up version of your first query loop:

if(have_posts()) {
    while ($sticky_query->have_posts()) {
        $sticky_query->the_post();
        if(is_sticky()) {
            ?>
                <div class="slide"><a rel="bookmark" href="<?php the_permalink(); ?>"><?php echo the_post_thumbnail('full'); ?><h2><?php the_title(); ?></h2></a></div>
            <?php
        }
    }
} else {
    ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php
}
wp_reset_postdata();

You can immediately see that your first check is if the main query has posts, not your sticky query, the main query.

You then immediatley follow this with a query_posts loop, here is a cleaned up version:

wp_reset_query();
query_posts('showposts=3&paged='.$paged);
?>
<?php
if(have_posts()) {
    while(have_posts()) {
        the_post();
        ?>
        <div class="clearfix"><li>
            <h4><a rel="bookmark" href="<?php the_permalink(); ?>">
                <?php echo the_post_thumbnail('full'); ?>
                <span><?php the_title(); ?></span>
            </a></h4>
            <?php the_excerpt(); ?>
        </li></div>
        <?php
    }
} else {
    ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php
}
wp_reset_query();

Immediatley you call wp_reset_query, despite not having a query to clean up. This makes no sense.

Then you go on to do a query_posts call. As mentioned earlier, this is incredibly bad practice. Never use query_posts. Instead consider using WP_Query as you did earlier.

So to summarise:

  • Unless you have a good IDE and a very strong grasp of your semantic structure, dont use alt syntax while: endwhile; hint: if you think you can, you probably can't
  • Unnecessary tags should be eliminated. It's ugly, it obscures your code, and it's extra time spent typing. Separate out your html and your PHP logic, and dont do logic and processing in the middle of a html structure when possible
  • Never, under any circumstances, use query_posts. There is no valid usage that is not covered by WP_Query or the pre_get_posts filter
  • NEVER use query_posts
  • Cleaning up after queries is good, but over-cleaning is bad and can interfere with other code
  • Consistent code indentation is important
share|improve this answer
2  
Everyone using query_posts() should take a look at this excellent Q&A. Add it to your favorites if you want a quick reference later. It definitely converted me, as I was also using query_posts() a lot before. –  Marc Dingena Mar 26 '13 at 14:35

I see potential bug here:

<?php $sticky_query = new WP_Query('nopaging=true'); ?>
<?php if(have_posts()) : while ($sticky_query->have_posts()) : $sticky_query->the_post(); ?>

Did you mean if($sticky_query->have_posts())?

EDIT

Also, as Tom J Nowell mentioned, you have that wp_reset_query(), which, I guessed, can't do anything, because you have nothing to reset. BUT! You are using query_var paged, so, you are maybe resetting that var too, which changes your sub-queries. Remove that definitely.

share|improve this answer
2  
Yeah, the check is wrong, but it won't cause the problem. –  kaiser Mar 26 '13 at 14:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.