A
A
avantus_rus2015-08-02 13:53:30
PHP
avantus_rus, 2015-08-02 13:53:30

How to optimize the output of recommended reading materials?

There is a site on WP. In the article, after the text, a block of articles recommended for reading is displayed. They are output as follows:

  • 2 links to previous articles in order of publication;
  • 2 links to the following articles in order of publication;
  • 2 links to previous articles in order of publication by tags;
  • 2 links to the following in the order of publication of the article by tags;

At the same time, if it turns out that the articles are duplicated by tags and simply in order, then the duplicating one is removed from the links in order and the next one is taken.
I did this by picking up scraps of information on the net. But I suspect that it turned out not very productive.
When this block is displayed, 23 queries to the database are added (looked at a specific article), memory consumption increases by 2.55MB, time by 0.018 sec.
I output the number of requests and so on like this: SQL - <?php echo get_num_queries (); ?> | <?php timer_stop(1); ?> sec. | <?php echo round (memory_get_usage ()/1024/1024, 2) ?> MB
See what I did below and please give me advice on how it can be optimized. Or maybe I'm worrying for nothing and everything can be left like that?
<?php 
global $SMTheme;
if ($SMTheme->get( 'layout', 'related' )) { ?>
        <div class="related-posts"><p></p><ul>
    <?php
    $postid=$post->ID;
    $showed_posts=array();
    $tags=get_the_tags($postid);
    $categories = get_the_category($postid);
    $post_cnt=0;
    $first=0;
    if ($tags) {
      $tags_ids = array();
      foreach($tags as $tag) $tags_ids[] = $tag->term_id;
      $args1=array(
      'tag__in' => $tags_ids,
      'showposts'=>$SMTheme->get( 'layout', 'relatedcnt' ),
      'caller_get_posts'=>1);
      $posts1=get_posts($args1);
if(count($posts1)>4){
$number1 = 0;
for($i=0; $i<count($posts1); $i++)
{
  $item = $posts1[$i];
  if($item->ID==$post->ID) $number1 = $i;

}
$arr1 = array();
$array_id = array();

for($i=$number1; $i<count($posts1);$i++)
{
  $item = $posts1[$i];
  $arr1[] = $item;
  $array_id[] = $item->id;
  
}

for($i=0; $i<$number1;$i++)
{
  $item = $posts1[$i];
  $arr1[] = $item;
  $array_id[] = $item->id;
}

$massiv = array();

for($i=1; $i<3; $i++)
{
  $massiv[]= $arr1[$i]->ID;
  $r=rand(1,150);
  $g=rand(1,150);
  $b=rand(1,150);
  $thumb_id = get_post_thumbnail_id($arr1[$i]->ID);
    $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail', true);
  $usl=get_post_meta($arr1[$i]->ID, '_yoast_wpseo_title', true);
  if($usl==null){$ankor=$arr1[$i]->post_title;}
  else{$ankor=$usl;}
  $prin1='<li><a href="'.get_permalink($arr1[$i]->ID).'" class="img related-ttl" style="background-image:url('.$thumb_url[0].');"><span class="related-bg" style="background:rgba('.$r.",".$g.",".$b.', 0.7)">'.$ankor.'</span></a></li>';
  print $prin1;
}

for($i=0; $i<2; $i++)
{   
    $arr11=array_reverse($arr1);
  $massiv[]= $arr11[$i]->ID;
  $r=rand(1,150);
  $g=rand(1,150);
  $b=rand(1,150);
  $thumb_id = get_post_thumbnail_id($arr11[$i]->ID);
    $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail', true);
  $usl=get_post_meta($arr11[$i]->ID, '_yoast_wpseo_title', true);
  if($usl==null){$ankor=$arr11[$i]->post_title;}
  else{$ankor=$usl;}
  $prin1='<li><a href="'.get_permalink($arr11[$i]->ID).'" class="img related-ttl" style="background-image:url('.$thumb_url[0].');"><span class="related-bg" style="background:rgba('.$r.",".$g.",".$b.', 0.7)">'.$ankor.'</span></a></li>';
  print $prin1;
}

foreach($massiv as $v){
      $massiv12[]=intval($v);
  }
  
}
    }
if (!empty($massiv12)){$massiv12=$massiv12;}
else {$massiv12=array(1);}

$category_ids = array();
      foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
      $showed_posts[]=$postid;
      $args=array(
      'category__in' => $category_ids,
      'post__not_in' => $massiv12,
      'showposts'=>$SMTheme->get( 'layout', 'relatedcnt' ),
      'caller_get_posts'=>1);
      $posts=get_posts($args);
      
$number = 0;
for($i=0; $i<count($posts); $i++)
{
  $item = $posts[$i];
  if($item->ID==$post->ID) $number = $i;
}
$arr = array();
for($i=$number; $i<count($posts);$i++)
{
  $item = $posts[$i];
  $arr[] = $item;
}

for($i=0; $i<$number;$i++)
{
  $item = $posts[$i];
  $arr[] = $item;
}



for($i=1; $i<3; $i++)
{
  $r=rand(1,150);
  $g=rand(1,150);
  $b=rand(1,150);
  $thumb_id = get_post_thumbnail_id($arr[$i]->ID);
    $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail', true);
  $prin='<li><a href="'.get_permalink($arr[$i]->ID).'" class="img related-ttl" style="background-image:url('.$thumb_url[0].');"><span class="related-bg" style="background:rgba('.$r.",".$g.",".$b.', 0.7)">'.$arr[$i]->post_title.'</span></a></li>';
  print $prin;
}

for($i=0; $i<2; $i++)
{   
    $arr111=array_reverse($arr);
  $r=rand(1,150);
  $g=rand(1,150);
  $b=rand(1,150);
  $thumb_id = get_post_thumbnail_id($arr111[$i]->ID);
    $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail', true);
  $prin='<li><a href="'.get_permalink($arr111[$i]->ID).'" class="img related-ttl" style="background-image:url('.$thumb_url[0].');"><span class="related-bg" style="background:rgba('.$r.",".$g.",".$b.', 0.7)">'.$arr111[$i]->post_title.'</span></a></li>';
  print $prin;
}		
  
    
      ?></ul></div>
  <?php } ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2015-08-02
@xmoonlight

At the same time, if it turns out that the articles are duplicated by tags and simply in order, then the duplicating one is removed from the links in order and the next one is taken.
Have you tried reverse logic?

I
Igor Vorotnev, 2015-08-02
@HeadOnFire

You use the standard functions to access posts in the database, enable the caching backend (Memcached, Redis) and everything will be ok. Another option is to use the Transients API to cache the result of all these requests at once, for example, for 1 day.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question