• Resolved Nathan Rice

    (@nathanrice)


    I can usually troubleshoot issues pretty easily, but this one has me stumped.

    I’m working on an old site, 5 years of content, over 4000 posts, loads of comments and images, etc. It’s completely up to date, no strange plugins active, and just to make sure it wasn’t a theme issue, I did my testing in TwentyTwelve.

    The homepage has been running quite slow lately, so I did some debugging.

    First, the relevant debug bar query results:

    SELECT SQL_CALC_FOUND_ROWS w1L7f03Z_posts.ID FROM w1L7f03Z_posts WHERE 1=1 AND w1L7f03Z_posts.post_type = 'post' AND (w1L7f03Z_posts.post_status = 'publish' OR w1L7f03Z_posts.post_status = 'private') ORDER BY w1L7f03Z_posts.post_date DESC LIMIT 0, 12
    
    SELECT FOUND_ROWS()

    Nothing too strange here. I have 12 selected as my posts per page in Reading settings.

    But, when I go to load the page, it takes FOREVER … like it’s literally trying to loop through and display every post in the database. Honestly, I never let it finish, but I suspect this is exactly what’s happening … it is trying to loop through all 4000+ posts.

    So, before the loop in index.php (twentytwelve), I pasted this in:

    <?php
    global $wp_query;
    echo 'Debugging. Nothing to see here.<br /><br />';
    echo '$wp_query = ' . count( $wp_query->posts );
    echo '</div></div>';
    wp_footer();
    exit;
    ?>

    This is how I got my debug bar results.

    Sure enough, the count() on the $wp_query->posts result is 4337.

    Does anyone have any idea what could be causing this? I can provide other info, if it will help.

    FWIW, I disabled all plugins and it’s still happening, so I don’t think I can blame a theme or plugin.

Viewing 6 replies - 16 through 21 (of 21 total)
  • I checked your query against a WP install with 2500+ posts, and got this:

    Showing rows 0 - 11 (12 total, Query took 0.0002 sec)

    So, the problem isn’t in the query syntax. Works as expected. I think you need to let WP complete the run (you said you stopped it prematurely above), and see what the actual output is. MySQL doesn’t appear to be the bottleneck, unless you have a problem in your server’s install. The only way to know for sure would be to run it on your own server, against the actual database… if you don’t have that skillset, ask a trusted colleague. Gotta be something in the PHP side of things.

    Sorry I couldn’t be more help. ??

    That was WITH the SQL_CALC_FOUND_ROWS command, btw, so that wasn’t slowing things down noticeably.

    Posted this on Twitter, but for posterity:

    I would guess mu-plugins and a pre_get_posts filter that alters posts_per_page

    And to elaborate: the fact that it works when you pass in author/category probably means that something is making posts_per_page unlimited (or very high) for the home page specifically.

    The query looks fine — are you sure that’s the right query you’re looking at?

    Hit the permalinks page in: Dashboard > Settings > Permalinks and give them a save. Maybe some old rewrite rules in there are causing havoc from before the fancy new logic was put in.

    Thread Starter Nathan Rice

    (@nathanrice)

    Thanks to Mark Jaquith, this issue is resolved.

    Turns out, because we use sticky posts to populate a slider, and I never thought to clear out posts from being sticky after they no longer need to be featured, we had 4000+ sticky posts. The main query was pulling all these guys in, causing the massive query mentioned above.

    Cleared out the stickies (and wrote up a limiter so this doesn’t happen again) and we’re back to smooth queries.

    Thanks so much to Mark for hunting this down!

    I’ve just ran into the same thing. Raw query resulted in two posts, actual WP_Query results 10 posts. It was those sticky posts.
    Thanks for figuring this out guys, saved me a couple more hours ??

Viewing 6 replies - 16 through 21 (of 21 total)
  • The topic ‘Main $wp_query returning WAY too many posts.’ is closed to new replies.