Reduce Database Queries in Code [duplicate]

Because this is very similar to

How to reduce the database query-es

I will then give you an outline of a possible solution:

You have four similar loops with

1) 1 post && offset=0 
2) 4 posts && offset=1 
3) 5 posts && offset=1 
4) 6 posts && offset=1 

so your main query will be

$args=array( 
    'post_type' => 'stiri',
    'posts_per_page' => 7,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
);

$recentPosts = new WP_Query($args);

Using that:

  • $recentPosts->current_post gives you the position (0,1,...) in
    the loop,
  • $recentPosts->rewind_posts() rewinds the posts,
  • $recentPosts->next_post() increments the position,

you can try out this structure for your loop with 4 posts and offset 1:

<?php $recentPosts->rewind_posts();?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <?php if($recentPosts->current_post>0 && $recentPosts->current_post<5):?>
      ...   
    <?php endif; ?>
<?php endwhile; ?>

or

<?php $recentPosts->rewind_posts();?>
<?php $recentPosts->next_post(); // to get offset 1 ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <?php if($recentPosts->current_post<5):?>
      ...   
    <?php endif; ?>
<?php endwhile; ?>

or

<?php $recentPosts->rewind_posts();?>
<?php $recentPosts->current_post=0; // to get offset 1 ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <?php if($recentPosts->current_post<5):?>
      ...   
    <?php endif; ?>
<?php endwhile; ?>

You can use this on the other loops as well, where you change the if-condition to your needs.

ps: Finally you should use

<?php wp_reset_postdata(); ?>

to restore the global $post to the current post in the main query, since it was altered with the above loops.

Update:

Place this before your div code

<?php
$args=array( 
    'post_type' => 'stiri',
    'posts_per_page' => 7,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
);
$recentPosts = new WP_Query($args);
?>

There is something strange in your div structure, so I give you these two examples:

You can use this as your dbr div block:

<div class="dbr">
<?php $recentPosts->rewind_posts();?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <!-- #5 posts / offset=1 --->
    <?php if($recentPosts->current_post >= 1 && $recentPosts->current_post <= 5):?>
        <div class="dbrs">
            <div class="dbrsi"><a href="https://wordpress.stackexchange.com/questions/88572/<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=77&amp;w=218&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a></div>
            <div class="dbrst"><a href="https://wordpress.stackexchange.com/questions/88572/<?php the_permalink() ?>"><?php the_title(); ?></a></div>
        </div>
    <?php endif; ?>     
<?php endwhile; ?>
</div>

and similarly for the dbm div block:

<div id="dbm">
<?php $recentPosts->rewind_posts();?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <!-- #4 posts / offset=1 --->
    <?php if($recentPosts->current_post >= 1 && $recentPosts->current_post <= 4):?>
        <div class="dbmc">
            <div class="dbmci"><a href="https://wordpress.stackexchange.com/questions/88572/<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=77&amp;w=143&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a></div>
            <div class="dbmct"><a href="https://wordpress.stackexchange.com/questions/88572/<?php the_permalink() ?>"><?php the_title(); ?></a></div>
        </div>
    <?php endif; ?>     
<?php endwhile; ?>
</div>

you can then do similarly for the other two cases. Hope this helps.

Leave a Comment