Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Fetch Custom Field from Page, display Tag WordPress


The following is the Custom Field that I use to fetch custom fields that are attached to pages.

function value_country_listing() {
if (is_page('9898')) {
<ul class="value">
<li><span><strong>VALUE in U.S. DOLLARS</strong></span></li></ul>
<ol class="ranking">
global $wpdb;
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'Value'
AND wposts.post_type = 'page'
ORDER BY ABS(SUBSTRING(wpostmeta.meta_value, 2)) DESC


$pages = $wpdb->get_results($querystr, OBJECT);
foreach($pages as $page)
{ ?>

<li><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a><span><?php echo get_post_meta($page->ID,'Value',true); ?></span></li>

<?php }}


Right now, it displays the Page when the custom field listed. Instead of the page, I would like my page to display the Tag that is attached to the page. Is this possible?

Answers (2)


Arnav Joy answers:

to send the link to tag.php , you can use following syntax

<a href="<?php echo bloginfo('url');?>/?tag=China " >China</a>

but this tag China must be registered as one of the tags

so in your case you can do it as

foreach($pages as $page)

{ ?>

<li><a href="<?php echo bloginfo('url');?>/?tag=<?php echo get_post_meta($page->ID,'Value',true); ?>"><?php echo get_post_meta($page->ID,'Value',true); ?></a></li>




vitaminman comments:

Hi Arnav,

Using your code, I was able to get to the following:

<li><a href="<?php echo bloginfo('url');?>/?tag=<?php echo get_post_meta($page->ID,'Value',true); ?>""><?php echo $page->post_title ?></a><span><?php echo get_post_meta($page->ID,'Value',true); ?></span></li>

The problem that I'm still having is to display the Tag. Now, the link displays the following:$620

The $620 is the value of the page.

Any suggestions?

Arnav Joy comments:

if $620 is the value of the page then you have written $620 in the custom field , then where it should redirect?

if you write China as its vale then it will take China as its value.


Michael Caputo answers:

What is the output of:

$output = get_post_meta($page->ID,'Value',true);

vitaminman comments:

Sorry, I don't understand what you mean by "output".

I have a page called "China", "Brazil," etc... I have Custom Fields on those pages. I call the fields "Value."

Using the code above, I display all countries with their "Value".

However, I'm changing our theme, and to make it easier, I don't want to write code for each Page to display data. Instead, I'm just using the Tag. So I don't want to send people to the "China" page, but rather the "China" Tag. But I don't know how to attach Custom Fields to Tags, so I was wondering is there is a way to keep the custom fields with the page "China", fetch the custom field, but instead of linking to the page China, to link to the tag/China.

Hope this makes sense.