WordPress retrieve custom field from outside the loop


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



Copy this code and paste it in your HTML
  1. <ul class="feature-images">
  2. <?php
  3. global $wpdb;
  4.  
  5. $sql = "SELECT wposts.*
  6. FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
  7. WHERE wposts.ID = wpostmeta.post_id
  8. AND wpostmeta.meta_key = 'lab-image'
  9. AND wposts.post_status = 'publish'
  10. AND wposts.post_type = 'post'
  11. ORDER BY wposts.post_date DESC LIMIT 3";
  12.  
  13. $pageposts = $wpdb->get_results($sql, OBJECT);
  14. $output = $pre_HTML;
  15.  
  16. foreach ($pageposts as $post) : setup_postdata($post); ?>
  17. <li><a href="<?php the_permalink(); ?>" title="Link to <?php the_title(); ?>">
  18. <img src="<?php echo get_post_meta($post->ID, "lab-image", $single = true); ?>" alt="<?php the_title(); ?>" /></a></li>
  19. <?php endforeach; ?>
  20.  
  21. </ul>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.