For the search results page Im making a custom template. And in that custom template I want to display custom meta field values.
However, when I do this:
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
$pName = get_post_meta($post->ID, $productName, 'true');
echo $pName;
);
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif;
The result I get is not the value of the custom field but:
Array
When I var_dump the $pName it shows all the right custom field content.
Question: Why am I getting an array as the result (when Ive told it its a single result with 'true') and how do I fix it so it displays the proper content?
Thanks!
--- UPDATE #1 ---
var_dump($pName) with true set:
array(12) { ["productName"]=> array(1) { [0]=> string(10) "Lemon Kush" } ["locationName"]=> array(1) { [0]=> string(57) "La Brea Caregivers THE BEST $35 CAP in the 90019 Zip Code" } ["locationAddress"]=> array(1) { [0]=> string(18) "1188 S La Brea Ave" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(11) " California" } ["locationZip"]=> array(1) { [0]=> string(6) " 90019" } ["locationPhone"]=> array(1) { [0]=> string(15) "1(323)-549-0400" } ["meta_priceGram"]=> array(1) { [0]=> string(0) "" } ["meta_priceEighth"]=> array(1) { [0]=> string(0) "" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } string(0) "" array(12) { ["productName"]=> array(1) { [0]=> string(10) "Lemon Kush" } ["locationName"]=> array(1) { [0]=> string(57) "La Brea Caregivers THE BEST $35 CAP in the 90019 Zip Code" } ["locationAddress"]=> array(1) { [0]=> string(18) "1188 S La Brea Ave" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(11) " California" } ["locationZip"]=> array(1) { [0]=> string(6) " 90019" } ["locationPhone"]=> array(1) { [0]=> string(15) "1(323)-549-0400" } ["meta_priceGram"]=> array(1) { [0]=> string(1) "5" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "15" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } string(0) "" array(12) { ["productName"]=> array(1) { [0]=> string(39) "Bananna Kush ***NEW*** 5grm 1/8 Special" } ["locationName"]=> array(1) { [0]=> string(10) "H+H CO-OP " } ["locationAddress"]=> array(1) { [0]=> string(21) "1938 West. Adams blvd" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90018" } ["locationPhone"]=> array(1) { [0]=> string(12) "323-733-0442" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "14" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "55" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } string(0) "" array(12) { ["productName"]=> array(1) { [0]=> string(37) "Master Kush 5grm 1/8 Special **NEW**" } ["locationName"]=> array(1) { [0]=> string(10) "H+H CO-OP " } ["locationAddress"]=> array(1) { [0]=> string(21) "1938 West. Adams blvd" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90018" } ["locationPhone"]=> array(1) { [0]=> string(12) "323-733-0442" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "10" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "50" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } string(0) "" array(12) { ["productName"]=> array(1) { [0]=> string(7) "OG Kush" } ["locationName"]=> array(1) { [0]=> string(30) "The Dragon Collective $45 CAP " } ["locationAddress"]=> array(1) { [0]=> string(16) "3977 West 6th st" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90020" } ["locationPhone"]=> array(1) { [0]=> string(12) "213-529-4016" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "40" } ["meta_priceEighth"]=> array(1) { [0]=> string(0) "" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } string(0) ""
var_dump($pName) true unset:
array(12) { ["productName"]=> array(1) { [0]=> string(10) "Lemon Kush" } ["locationName"]=> array(1) { [0]=> string(57) "La Brea Caregivers THE BEST $35 CAP in the 90019 Zip Code" } ["locationAddress"]=> array(1) { [0]=> string(18) "1188 S La Brea Ave" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(11) " California" } ["locationZip"]=> array(1) { [0]=> string(6) " 90019" } ["locationPhone"]=> array(1) { [0]=> string(15) "1(323)-549-0400" } ["meta_priceGram"]=> array(1) { [0]=> string(0) "" } ["meta_priceEighth"]=> array(1) { [0]=> string(0) "" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } Array array(0) { } Array array(12) { ["productName"]=> array(1) { [0]=> string(10) "Lemon Kush" } ["locationName"]=> array(1) { [0]=> string(57) "La Brea Caregivers THE BEST $35 CAP in the 90019 Zip Code" } ["locationAddress"]=> array(1) { [0]=> string(18) "1188 S La Brea Ave" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(11) " California" } ["locationZip"]=> array(1) { [0]=> string(6) " 90019" } ["locationPhone"]=> array(1) { [0]=> string(15) "1(323)-549-0400" } ["meta_priceGram"]=> array(1) { [0]=> string(1) "5" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "15" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } Array array(0) { } Array array(12) { ["productName"]=> array(1) { [0]=> string(39) "Bananna Kush ***NEW*** 5grm 1/8 Special" } ["locationName"]=> array(1) { [0]=> string(10) "H+H CO-OP " } ["locationAddress"]=> array(1) { [0]=> string(21) "1938 West. Adams blvd" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90018" } ["locationPhone"]=> array(1) { [0]=> string(12) "323-733-0442" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "14" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "55" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } Array array(0) { } Array array(12) { ["productName"]=> array(1) { [0]=> string(37) "Master Kush 5grm 1/8 Special **NEW**" } ["locationName"]=> array(1) { [0]=> string(10) "H+H CO-OP " } ["locationAddress"]=> array(1) { [0]=> string(21) "1938 West. Adams blvd" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90018" } ["locationPhone"]=> array(1) { [0]=> string(12) "323-733-0442" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "10" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "50" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } Array array(0) { } Array array(12) { ["productName"]=> array(1) { [0]=> string(7) "OG Kush" } ["locationName"]=> array(1) { [0]=> string(30) "The Dragon Collective $45 CAP " } ["locationAddress"]=> array(1) { [0]=> string(16) "3977 West 6th st" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90020" } ["locationPhone"]=> array(1) { [0]=> string(12) "213-529-4016" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "40" } ["meta_priceEighth"]=> array(1) { [0]=> string(0) "" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } Array array(0) { }
--- UPDATE #2 ---
If it makes any difference or not: the post im want the meta from are in a CPT (product) (but Im using the 'search everything' plugin to include CPT's in standard search results)
-- FIGURED IT OUT!! --
Ok, I made a really silly error.
$productName should not be a var. It should be the actual name of the field, which is: productName.
So making that change fixed all the problems.
haha.. good times :)
Thanks for all your help everybody!
get_post_meta($post->ID, $productName, 'true';
is missing an end)