0

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!

  • This could be a mistype, but it looks like get_post_meta($post->ID, $productName, 'true'; is missing an end ) – Howdy_McGee May 14 '14 at 21:01
  • yeh I missed the last little bit when I copied and pasted. Ive fixed it in the original post now. – user12920 May 14 '14 at 21:38
1

You've got a missing ) and you're passing true as a string and not a boolean value. So this line:

$pName = get_post_meta($post->ID, $productName, 'true';

Should in fact be:

$pName = get_post_meta($post->ID, $productName, true);

In light of your update why not do something like:

echo 'Product Name: '. $pName['productName'][0];
echo 'Location: '. $pName['locationName'][0];
echo 'Address: '. $pName['locationAddress'][0];

However this code will need to be improved and is maybe a question for stackoverflow as it's not specific to WordPress. This is PHP and dealing with arrays/multidimensional arrays

  • Ok, I fixed that (changed 'true' to true) but I still get the same results. – user12920 May 14 '14 at 21:40
  • Could you possibly do a var_dump($pName); both with true set and with true omitted and edit your question with both results. That way we can see the state of the data returned – Craig Pearson May 14 '14 at 21:44
  • Ok, Ive updated the original question with var_dump results for both cases. – user12920 May 14 '14 at 21:48
  • The function is acting correctly, you have 2 arrays stored under one post_meta key. One array is populated, and the second array is empty. So when set to true it returns the first array discarding the last empty array. I think you're going to need a different approach to getting the result you want. Maybe you should iterate through the returned array to get the data you need – Craig Pearson May 14 '14 at 21:57
  • Any suggestions? – user12920 May 14 '14 at 22:12
0

I'm assuming that your meta_data is a serialized array. Here's what the get_post_meta() documentation says about serialized arrays:

$single
(boolean) (optional) If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields. This may not be intuitive in the context of serialized arrays. If you fetch a serialized array with this method you want $single to be true to actually get an unserialized array back. If you pass in false, or leave it out, you will have an array of one, and the value at index 0 will be the serialized string.

So if you want an actual single value returned you will have to actually save out each meta one by one instead of updating the meta as an array entirely. To access these values you can use a foreach or access them directly via indexes starting at 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.