• Hi,

    I’m using the following code to pull random posts for a sidebar widget. I have execute php plugin installed and am using a text widget to input the code.

    <?php
     global $post;
     $rand_posts = get_posts('numberposts=10&orderby=rand&category=3,6');
     foreach( $rand_posts as $post ) :
     ?>
    <li>
    
     <?php
    $posttags = get_the_tags();
    if ($posttags) {
    foreach($posttags as $tag) {
    echo '<img src="www.xxx.com' . $tag->slug  . '.jpg" alt="' . $tag->name . '" title=" ' . $tag->name. '" />';
    }
    }
    
    ?>
    
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    
     <?php endforeach; ?>
    
    </ul>

    So basically, I’m pulling posts from 1 of 2 categories, outputting my tag picture and then the post title. I’m outputting 10 posts. It works great. Here’s the problem.

    When its installed, I notice through wp tuner that it takes up 113 ms and 62% of my database time with only 6% of the database count. When I remove the sidebar widget, my database time drops by 95% to only 12 ms.

    Now, I have used the random posts plugin but this was causing the same sort of time issues. My host is saying I’m taking up too much cpu usage even with wp super cache. Is there a way I can fine tune the above code so it will work smoother? Or is there another way to execute this so it doesn’t take up so much database time?

    Thanks,

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Random Posts Query’ is closed to new replies.