Как уменьшить количество запросов к базе с 4 до 1 в следующем коде
<h3>Обучиться</h3> <?php $categories = get_the_category($post->ID); if ($categories) { $category_ids = array( 8 ); foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id; $args=array( 'category__in' => $category_ids, 'orderby'=>rand, 'post__not_in' => array($post->ID), 'showposts'=>3, 'caller_get_posts'=>1); $my_query = new wp_query($args); if( $my_query->have_posts() ) { echo '<ul>'; while ($my_query->have_posts()) { $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php } echo '</ul>'; } wp_reset_query(); } ?></div></div> <div class="torgov"> <div class="drugiezapisi"> <h3>Торговать</h3> <?php $categories = get_the_category($post->ID); if ($categories) { $category_ids = array( 9 ); foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id; $args=array( 'category__in' => $category_ids, 'orderby'=>rand, 'post__not_in' => array($post->ID), 'showposts'=>3, 'caller_get_posts'=>1); $my_query = new wp_query($args); if( $my_query->have_posts() ) { echo '<ul>'; while ($my_query->have_posts()) { $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php } echo '</ul>'; } wp_reset_query(); } ?></div></div><div class="clearerist"></div></div> <? } ?> <h3>Советы</h3> <?php $categories = get_the_category($post->ID); if ($categories) { $category_ids = array( 10 ); foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id; $args=array( 'category__in' => $category_ids, 'orderby'=>rand, 'post__not_in' => array($post->ID), 'showposts'=>3, 'caller_get_posts'=>1); $my_query = new wp_query($args); if( $my_query->have_posts() ) { echo '<ul>'; while ($my_query->have_posts()) { $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php } echo '</ul>'; } wp_reset_query(); } ?> <h3>Как заработать</h3> <?php $categories = get_the_category($post->ID); if ($categories) { $category_ids = array( 11 ); foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id; $args=array( 'category__in' => $category_ids, 'orderby'=>rand, 'post__not_in' => array($post->ID), 'showposts'=>3, 'caller_get_posts'=>1); $my_query = new wp_query($args); if( $my_query->have_posts() ) { echo '<ul>'; while ($my_query->have_posts()) { $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php } echo '</ul>'; } wp_reset_query(); } ?>
Спасибо!
и вообще - Код нужно оптимизировать.
Много повторов.
Используете new wp_query($args);
а нужно get_posts( $args )
Просто. Нужно использовать кеш.
То есть, все данные, которые выводятся в Вашем коде нужно сохранить в переменной и потом
Вопрос появится, как же и когда именно обновлять кеш ?
Здесь уже посложнее.
1) когда создаются/редактируются/удаляются новые категории (хук не помню, в гугле найдете)
2) когда срабатывает хук save_posts - то есть, создается/редактируется пост
3) когда удаляется пост - там свой хук
Вместо get_option можно использовать
set_transient