We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,745 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP issue in my blog

After this piece of PHP code is added to my template (Wordpress) my server goes bananas. (I see lots of load peaks, it becomes unresponsive and the site won't load until I reboot the server.)
The code just displays random posts from the database:

<?php
$random_query = new WP_Query('posts_per_page=4&orderby=rand');
while($random_query->have_posts()) : $random_query->the_post(); global $post; ?>

	<li>
	<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="http://mydomain.com/images/<?php echo get_post_meta($post->ID, 'thumbnail', true); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a>
	</li>

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

Does anyone know how can I optimize this piece of code to avoid having all these problems?

3
Contributors
2
Replies
1 Year
Discussion Span
1 Month Ago
Last Updated
18
Views
zglows
Newbie Poster
1 post since Apr 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Does anyone know how can I optimize this piece of code to avoid having all these problems?

You can read this:

http://www.wpbeginner.com/wp-tutorials/speed-up-your-wordpress-by-caching-custom-queries-using-transients-api/

as for you issue I think someone provide a nice code here:

http://wordpress.org/support/topic/wordpress-php-code-problems

LastMitch
Industrious Poster
4,132 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

you dont need to instanciate a new wp_query if you are in your template fine.. you just need to use query_post() function with arguments.

Here is the codex. link

 <?php
// The Query
query_posts( $args );

// The Loop
while ( have_posts() ) : the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Query
wp_reset_query();
?> 
gabrielcastillo
Junior Poster in Training
54 posts since Apr 2012
Reputation Points: 0
Solved Threads: 5
Skill Endorsements: 0

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0715 seconds using 2.7MB