Connect with us

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.

4 Comments

4 Comments

  1. Pingback: Weekend Links - Jan 23, 2009 | OMNINOGGIN

You must be logged in to post a comment Login

Random Search Terms

Title

Top Articles

powered by WassUp

Recent Posts: Fully Net Worth . com

Alex Scott Net Worth: How Rich is Alex Scott Actually?

Alex Scott Net Worth: How Rich is Alex Scott Actually?

Alex Scott is a former English footballer who is best known for her three stints at Arsenal as a right back. Moreover, she is the second most capped player in the history of the Women’s English national team with a total of 140 appearances for them. As of 2019, Alex Scott net worth is estimated […]

Reanne Evans Net Worth: How Rich is the English Snooker Player?

Reanne Evans Net Worth: How Rich is the English Snooker Player?

Reanne Evans is an English snooker player and the current World Women’s Snooker Champion. She has held the title a record 12 times, with 10 consecutive titles coming between 2005 and 2014. Evans is currently ranked number one on the World Women’s snooker Tour. As of 2019, Reanne Evans net worth is estimated to be £500,000. Evans was […]

Sasha Velour Net Worth: How Rich is the Drag Queen Actually?

Sasha Velour Net Worth: How Rich is the Drag Queen Actually?

Alexander Hedges Steinberg also known by the stage name Sasha Velour is an American drag queen and artist. Velour is best known for winning the 9th season of RuPaul’s Drag Race. As of 2019, Sasha Velour net worth is estimated to be $2.5 million. Velour was born on June 25, 1987, in Berkeley, California as the only child of Mark […]