Wordpress slow and with database access errors

Asked

Viewed 56 times

0

All right guys,

I had asked another question, but I was able to identify some queries that are consuming a lot of running time.

I developed a theme for a news portal, and in each area I upload news from a certain category. But as some posts (news) use more than one category, I need to be careful not to get repeated data, in this case I use the post__not_in option

Take an example:

<?php
//$posts_ID é uma variavel que guarda dos IDs dos posts ja exibidos em consultas anteriores a esta.
$posts_planeta_diario = new WP_Query(
    array(
        'post__not_in' => $posts_ID,
        'posts_per_page' => 3,
        'cat' => 3946
    )
);
$return_while = 1;
    while ($posts_planeta_diario->have_posts()):
        $posts_planeta_diario->the_post();

        //armazena os IDs resgatados 
        $posts_ID[] = get_the_ID();

        //evitar quebra de layout
        if ($return_while == 3)
            $style_planeta_diario = 'style="margin-right:0px"'
?>
        <li class="liNoticiasPlanetaDiario02" <?php echo $style_planeta_diario; ?>>
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumb-205x145') ?></a>
            <article>
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            </article>
        </li>

<?php 
        $return_while ++;
    endwhile; 
?>

In this example shows what happens on the home page of the site. My database currently has 1gb. When I have many simultaneous accesses of that famous database error (ERROR WHEN CONNECTING TO DATABASE).

The logic I used above is correct, it may be this type of call that is causing slowness and bank error ?

  • Guy apparently has nothing wrong with his logic, check whether in your wordpress has many crons. Basic question your application account server?

  • Probably the max_user_connections/max_connections Mysql, which limits the number of concurrent processes. If this is so, then these topics are related: http://answall.com/questions/182000/exceeded-the-max-user-connections-resource-c-sql/ and http://answall.com/questions/46433/mariadb-para-quando-tenho-muitos-acessos-no-wordpress.

1 answer

0

I don’t know if it’s been solved yet. But you forgot to put the wp_reset_postdata() at the end after the endwhile for more information on what to/why to use here

Another thing I didn’t understand right is what you have to increment the loop. That’s not good. You can do the same thing you did here $style_planeta_diario = 'style="margin-right:0px"' using asssim css only:

p:nth-child(3n) {
  margin-right:0px
}

Doing this is one less loop that your code will do and will greatly improve performance.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.