Wordpress slow and with database access errors

0

All good personal,

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

I developed a theme for a news portal, and in each area I upload news from a certain category. But since 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

See 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; 
?>

This example demonstrates 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 THE DATABASE).

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

    
asked by anonymous 03.04.2017 / 21:12

1 answer

0

I do not know if it has already been resolved. But you forgot to put wp_reset_postdata() at the end after endwhile for more info why / why use here

Another thing I did not understand right is that you have the need to increment the loop. This is not good. You can do the same thing you did here $style_planeta_diario = 'style="margin-right:0px"' using only css so:

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

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

    
10.05.2017 / 17:39