1

I've developed a frontend user center function(login,register,edit profile,add post,etc).Everything is fine and cool except when a user other than admin is logged in ,the homepage will be very slow in his view.

I debugged it somehow and find the culprit as below:

SELECT SQL_CALC_FOUND_ROWS wiki_posts.ID
FROM wiki_posts
WHERE 1=1
AND wiki_posts.post_type = 'post'
AND (wiki_posts.post_status = 'publish'
OR wiki_posts.post_author = 4 //the current user which is not admin
AND wiki_posts.post_status = 'private')
ORDER BY wiki_posts.post_date DESC
LIMIT 0, 20

This query took like 60 seconds! and It only happens on my hompage which is index.php in my case and loops 20 posts.

So my question is :

1) why wordpress do this query for a non-admin user evertime he logged in and try to view the homepage?

2).what might cause this? I pretty much sure about the loop in my index.php won't bring up this.

0

ok,so I digged into wp-includes/query.php,and found this on line 3180:

if ( is_user_logged_in() ) {
            // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
            $private_states = get_post_stati( array('private' => true) );
            foreach ( (array) $private_states as $state )
                $where .= current_user_can( $read_private_cap ) ? " OR $wpdb->posts.post_status = '$state'" : " OR $wpdb->posts.post_author = $user_id AND $wpdb->posts.post_status = '$state'";
        }

So this is the trigger and I can solve the performance problem by commenting the foreach.

But my question remains:why wordpress do this and what's the purpose?

We are talk about a huge site here (with more than 40k posts).

  • Instead of hacking core it would be safer to look into caching plugins, which will cache the homepage for a certain amount of time and prevent the lengthy query process for front end non-authenticated users. – WebElaine Mar 20 '17 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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