Custom Wordpress query using custom fields outside the loop

/ Published in: PHP
Save to your folder(s)

$quote -- name of function
post_type=page -- remove to query posts
orderby=rand -- random order
showposts=1 -- limit to 1 post

effect -- displays 1 random custom excerpt outside of the loop

Copy this code and paste it in your HTML
  1. <?php $quote = get_posts('post_type=page&showposts=1&meta_key=quote&orderby=rand'); foreach($quote as $post) : setup_postdata($post); ?>
  2. <li class="quote"><a href="<?php the_permalink(); ?>" title="Read the <?php the_title(); ?> case study"><?php echo get_post_meta($post->ID, "quote", true); ?></a>
  3. <em><?php echo get_post_meta($post->ID, "quote-author", true); ?></em></li>
  4. <?php endforeach; ?>

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.