• I’m using a custom theme (that I’ve been trying to code and I’m not supercoder) which shows a block with the most recent posts and from different categories I assigned at the bottom of the the post.

    The code worked for more than two years, but in the past two months (more or less), it’s been causing server problems, such as high CPU usage. Using the htop command in Linux I can see many mysql processes and some taking the most resources.

    I looked into MySQL using the show processlist;
    This is how it appears in the “Info” column:
    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.

    I narrowed it down to this block of code that queries the posts to show a list of recent posts at bottom of posts, but I would appreciate if anyone can help me why this code is causing the database to go crazy.

    <div class="sidebar-latest-inner">
    
    	<div class="widget-header"><h3 class="widget-title"><span class="first-word">Latest</span> Headlines</h3>
    		<div class="line"></div>
    		<a href="/blog" class="allcontent">
    			<div>
    			<i class="fa fa-th allcontent-button"></i>
    			<span class="first-word">See all</span>
    			<span>Headlines</span> 
    			</div>
    		</a>
    	</div>
    	<ul id="posts-latest-headlines" class="sidebar-latest-wrapper" >
      <?php $do_not_duplicate = array(); 
    		$IDOutsideLoop = $post->ID;
          global $post;
    		 
    
          $myposts = get_posts('showposts=4');
          foreach($myposts as $post) :
    		
    		  $do_not_duplicate[] = $post->ID;
          ?>
    		
          <li <?php
    		if(is_single() && $IDOutsideLoop == $post->ID) 
    			{
    			echo " class=\"latest-tiles widget-current\"";
    			}
    		else {
    			echo " class=\"latest-tiles\"";
    			}
    			?>>
              <a href="<?php the_permalink() ?>">
    
                   <!-- .article_image -->
                  <?php if(has_post_thumbnail()) :?> 
    						
    					    <figure class="background-content-latest latest-article-images" itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
    										<meta itemprop="url" content="<?php the_post_thumbnail_url( 'medium' ); ?>">
    										<?php
    											$imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium' );
    											$imgwidth = $imgdata[1]; // thumbnail's width
    											$imgheight = $imgdata[2]; // thumbnail's heigh
    											?>
    											<meta itemprop="width" content="<?php echo $imgwidth; ?>">
    											<meta itemprop="height" content="<?php echo $imgheight; ?>">
    
    											<?php the_post_thumbnail('medium'); ?>
    							</figure>
         
                  		<div class="wrap-latest">
    										<h2 class="entry-title"><?php the_title(); ?></h2>
                   	 	</div>
                   <?php else :?>
                     
                      	<h2 class="entry-title title-full"><?php the_title(); ?></h2>
    										
                   <?php endif;?><!-- .article_image -->
              </a>                       
       </li>
                                      
    	 <?php endforeach; ?><?php wp_reset_query(); ?>
        </ul> 
    	
    	</div>
    </section> 
    
     
    
    <section id="sidebar-grid-latest-wrapper">       
    <div class="sidebar-latest-inner">
    
    	<div class="widget-header"><h3 class="widget-title"><span class="first-word">How To</span> Stories</h3>
    		<div class="line"></div>
    		<a href="/tag/how-to" class="allcontent">
    			<div>
    			<i class="fa fa-th allcontent-button"></i>
    			<span class="first-word">See all</span>
    			<span>How To</span> 
    			</div>
    		</a>
    	</div>
    	<ul id="posts-howto-stories" class="sidebar-latest-wrapper">
    	
    	
    		
    		
      <?php 
    		$IDOutsideLoop = $post->ID;
          global $post;
    
          $myposts =  new WP_Query( array( 'tag_slug__in' => array('how-to'), 'posts_per_page' => 4, 'order' => 'DESC', 'post__not_in' => $do_not_duplicate ) );
    			while ($myposts->have_posts()) : $myposts->the_post();
    		
    		  $do_not_duplicate[] = $post->ID;
          ?>
    	
          <li <?php
    		if(is_single() && $IDOutsideLoop == $post->ID) 
    			{
    			echo " class=\"latest-tiles widget-current\"";
    			}
    		else {
    			echo " class=\"latest-tiles\"";
    			}
    			?>>
              <a href="<?php the_permalink() ?>" >
    
                   <!-- .article_image -->
                  <?php if(has_post_thumbnail()) :?> 
                  
    						  <figure class="background-content-latest latest-article-images" itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
    										<meta itemprop="url" content="<?php the_post_thumbnail_url( 'medium' ); ?>">
    										<?php
    											$imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium' );
    											$imgwidth = $imgdata[1]; // thumbnail's width
    											$imgheight = $imgdata[2]; // thumbnail's height
    											?>
    											<meta itemprop="width" content="<?php echo $imgwidth; ?>">
    											<meta itemprop="height" content="<?php echo $imgheight; ?>">
    
    											<?php the_post_thumbnail('medium'); ?>
    							</figure>
    						
                  		<div class="wrap-latest">
    										<h2 class="entry-title"><?php the_title(); ?></h2>
                   	 	</div>
                   <?php else :?>
                     
                      	<h2 class="entry-title title-full"><?php the_title(); ?></h2>
    										
                   <?php endif;?><!-- .article_image -->
              </a>                     
       </li>
                                      
    	 <?php endwhile; ?><?php wp_reset_query(); ?>
        </ul> 
    	
    	</div>
    </section> 
    
     
    <section id="sidebar-grid-latest-wrapper">       
    <div class="sidebar-latest-inner">
    
    	<div class="widget-header"><h3 class="widget-title"><span class="first-word">Feature</span> Stories</h3>
    		<div class="line"></div>
    		<a href="/category/features" class="allcontent">
    			<div>
    			<i class="fa fa-th allcontent-button"></i>
    			<span class="first-word">See all</span>
    			<span>Features</span> 
    			</div>
    		</a>
    	</div>
    	<ul id="posts-feature-stories" class="sidebar-latest-wrapper">
    	
    	
    		
    		
      <?php 
    		$IDOutsideLoop = $post->ID;
          global $post;
    
          $myposts =  new WP_Query( array( 'category_name' => 'features', 'posts_per_page' => 4, 'order' => 'DESC', 'post__not_in' => $do_not_duplicate ) );
    			while ($myposts->have_posts()) : $myposts->the_post();
    		
    		  $do_not_duplicate[] = $post->ID;
          ?>
    		
    	
          <li <?php
    		if(is_single() && $IDOutsideLoop == $post->ID) 
    			{
    			echo " class=\"latest-tiles widget-current\"";
    			}
    		else {
    			echo " class=\"latest-tiles\"";
    			}
    			?>>
              <a href="<?php the_permalink() ?>">
    
                   <!-- .article_image -->
                  <?php if(has_post_thumbnail()) :?> 
                  
    							<figure class="background-content-latest latest-article-images" itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
    										<meta itemprop="url" content="<?php the_post_thumbnail_url( 'medium' ); ?>">
    										<?php
    											$imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium' );
    											$imgwidth = $imgdata[1]; // thumbnail's width
    											$imgheight = $imgdata[2]; // thumbnail's height
    											?>
    											<meta itemprop="width" content="<?php echo $imgwidth; ?>">
    											<meta itemprop="height" content="<?php echo $imgheight; ?>">
    
    											<?php the_post_thumbnail('medium'); ?>
    							</figure>
    						
                  		<div class="wrap-latest">
    										<h2 class="entry-title"><?php the_title(); ?></h2>
                   	 	</div>
                   <?php else :?>
                     
                      	<h2 class="entry-title title-full"><?php the_title(); ?></h2>
    										
                   <?php endif;?><!-- .article_image -->
              </a>                     
       </li>
                                      
    	 <?php endwhile; ?><?php wp_reset_query(); ?>
        </ul> 
    	</div>
    • This topic was modified 4 years, 7 months ago by wpmhweb.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Help fixing theme code causing database problems’ is closed to new replies.