How to get custom field value using a shortcode

Just add this WordPress Code Snippet below to the functions.php file in your current WordPress theme to allow you to get custom field values by simply using a shortcode.

 * Get Custom Field Value Using A Shortcode
 * @author
 * @link
add_shortcode('value', 'shortcode_value');
function shortcode_value($atts){
                  'post_id' => NULL,
               ), $atts));
  if(!isset($atts[0])) return;
       $value = esc_attr($atts[0]);
       global $post;
       $post_id = (NULL === $post_id) ? $post->ID : $post_id;
       return get_post_meta($post_id, $value, true);

Then, inside your post, you can add this to insert the value from the “my_custom_field_key” custom field into the post:

[value “my_custom_field_key”]

Or, you can add this somewhere else on the site, in a page or another post for example, to get the value for the “my_custom_field_key” custom field from a particular post using the Post ID:

[value “my_custom_field_key” post_id=1]

* Replace my_custom_field_key above with your custom field key’s name.

   Related Sponsored Links

Disclaimer: The information contained in this website is for general information purposes only and in no event will this site or its owners be liable for any losses or damages associated with your use of our site or content. Click here to see our full Disclaimer.

Leave a Comment