Categories: Tips & Tricks

Pulling custom fields from outside the loop

In the last post “Creating a custom widget” I showed you how to create a custom widget. Well in this post I will show you how I used my custom widget to display all post with a certain custom field from outside the WordPress loop.

In the last post I used this tag:

<?php include(TEMPLATEPATH . '/includes/showcase.php'); ?>

Now I will show you what the file showcase.php has:

<ul>
<?php
global $wpdb;

$sql = "SELECT wposts.*
 FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
 WHERE wposts.ID = wpostmeta.post_id
 AND wpostmeta.meta_key = 'gallery-url'
 AND wposts.post_status = 'publish'
 AND wposts.post_type = 'post'
 ORDER BY wposts.post_date DESC LIMIT 8";

$pageposts = $wpdb->get_results($sql, OBJECT);
$output = $pre_HTML;

foreach ($pageposts as $post) : setup_postdata($post);  ?>

<li><a href="<?php echo get_post_meta($post->ID, "gallery-url", $single = true); ?>" title="Link to <?php the_title(); ?>">
<img src="<?php echo get_post_meta($post->ID, "image", $single = true); ?>" alt="<?php the_title(); ?>" /></a></li>

<?php endforeach; ?>

</ul>

That’s it! The most important item you may want to change for your own custom field is the line : AND wpostmeta.meta_key = 'gallery-url'. Where you would change the text in bold to match your own custom field value.

Update:

Check out Austin from PressedWords comment below.

With his great advise I was able to figure out why all my attempts to use the query_post weren’t working. it came down to this line of code: <?php echo get_post_meta($post->ID, "gallery-url", $single = true); ?>, that was what I had in my code, and the $post->ID is the reason my code would not echo or print the custom field’s value. Solution? replace $post->ID with get_the_ID(). HA, so simple.

Web Master

Hi, I am Miguel, I bought this site in 2009. So I now run or manage the site. Please visit my new website or follow me on twitter @W3i.

View Comments

  • No need to query the database directly. You can use the WordPress API to accomplish the same thing:

    <ul>
    <?php
    query_posts(array(
    'meta_key' => 'gallery-url',
    'showposts' => 8,
    ));
    while( have_posts() ) : the_post(); ?>

    <li><a href="<?php echo get_post_meta(get_the_ID(), "gallery-url", $single = true); ?>" title="<?php echo attribute_escape(sprintf('Link to %s', get_the_title())); ?>">
    <img src="<?php echo get_post_meta(get_the_ID(), "image", $single = true); ?>" alt="<?php echo attribute_escape(get_the_title()); ?>" /></a></li>

    <?php endwhile; ?>

    </ul>

    If you can, it's usually better to use the WordPress API, because your code won't have to change if the underlying database structure in WordPress changes (as it has before), you get the benefits of the built-in object caching (or caching plugins, if they're being used), and it tends to allow better compatibility with other plugins.

    Austin’s last blog post..WordPress Use Declines Among Top 100 Bloggers

  • Thanks for that, I will give that code a shot. I tried so many variations, and it wouldn't show the custom fields, just the html code I input.

    ;)

    P.S. If that works I will write a post based on your recommended code!

Recent Posts

21 Effective Strategies to Amplify Website Traffic

The article provides a comprehensive guide to driving traffic to your website, highlighting 21 effective…

2 hours ago

Essentials for Ecommerce Website Development

While e-commerce is projected to account for more than $6.5 trillion in sales by 2023,…

14 hours ago

17 Essential Link Building Statistics and Trends for Enhanced SEO

The analysis highlights the significance of link-building in SEO, revealing that most websites neglect backlinks,…

1 day ago

Get a copy of the book Blog Blazers!

Hey everyone, I've got two copies of Blog Blazers that I want to give away…

2 days ago

101 of the best blogging tools in 2024,

101 of the best blogging tools in 2024. Having the best blogging tools continues to…

2 days ago

Remove the title attribute using jQuery

In WordPress, when you use wp_page_menu your anchor attribute's usually carry a title with the…

3 days ago