I have made a custom template to show the posts in "Products" category in a single page.I have some custom fields.I need to show the values of the custom fields in the page. I have used the get_post_custom_values as the following but i get an error as

Warning: Invalid argument supplied for foreach() in C:\wamp\www\SampleSite\wp-content\themes\TwentyElevenChildTheme\products.php on line 11

The code for products.php is as follows.

Template Name: Product Template
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
  <?php $recent = new WP_Query("cat=4&showposts=10"); while($recent->have_posts()) :$recent->the_post();?>
  <?php $key_values = get_post_custom_values("Description"); ?>
  <?php foreach($key_values as $key => $value )?>
  <?php echo  "$key  => $value("Description") <br />";?>
  <?php endwhile; ?>
<?php get_footer(); ?>

I am not able to find what the error is.Someone please help me.

2 Answers 2


It most probably means that no custom post values have been retrieved for the key Description that you used. According to the Codex, Returns nothing if no such key exists, or none is entered.

Try adding print_r($key_values); on the line just after get_post_custom_values("Description"); and check if any value is retrieved.

To skip this warning(when you don't have any custom post values associated to a post), you should check if $key_values is an array before doing the foreach. And don't forget to escape your double quotes.

if( is_array( $key_values ) ){
  foreach($key_values as $key => $value );
  echo  "$key  => $value(\"Description\") <br />"; #You should escape your double quotes here!

You can try

<?php $key_values = get_post_custom_values("Description",get_the_ID()); ?>

ps: you should use posts_per_page instead of showposts in WP_Query().

You can use also debug it with


to find out all the custom fields in your post.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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