I am modifying a search.php template. The basic template displays search results with thumbnail, title, date, and author. I would like to add 2 additional fields from the wp_metadata table. I assume I need to write a function and add it to my child theme functions.php file, then call the function from the template. I just need to know how to correctly write the function.

  • wp_metadata ?? I think you are talking about wp_postmeta table – Aftab Aug 31 '16 at 13:27
  • Yes, you are correct. I meant to say wp_postmeta. Thanks! – David Cook Aug 31 '16 at 14:08

You need to use get_post_meta:

get_post_meta( $post_id, $key = '', $single = false )

Retrieve post meta field for a post

$post_id (int) (Required) Post ID.

$key (string) (Optional) The meta key to retrieve. By default, returns data for all keys. Default value: ''

$single (bool) (Optional) Whether to return a single value. Default value: false


echo get_post_meta( $post->ID, 'the-name-of-key', true); 

When using true, you will get a string, but if you use false instead, you will get an array.

| improve this answer | |
  • Thanks J....small syntax errors on my part. I was thaaaaat close. – David Cook Aug 31 '16 at 13:16
  • Glad I could help. If that's what you needed, dont forget to tick it as the correct answer. – The J Aug 31 '16 at 13:17

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.