Get a custom field value with shortcodes

Glenn Glenn's Blog 0 Comments

Many Custom Post creators like CustomPress by wpmu make the process of adding WordPress Custom Fields / Custom Meta pretty straightforward, but if you are using a traditional approach (or even with such plugins) you can use the following WordPress snippet to add a Custom Field to a page/post etc with a shortcode. Add the following to your them’s functions.php file and then see the usage underneath.

// Get a custom field value with shortcodes

add_shortcode('field', 'shortcode_field');

function shortcode_field($atts){
 'post_id' => NULL,
 ), $atts));

if(!isset($atts[0])) return;
 $field = esc_attr($atts[0]);

global $post;
 $post_id = (NULL === $post_id) ? $post->ID : $post_id;

return get_post_meta($post_id, $field, true);


[field “my_meta_id”]
[field “my_meta_id” post_id=1]

Note that this needs to be in the WordPress Loop.Custom Post

About the Author
Get a custom field value with shortcodes Get a custom field value with shortcodes 0da77002febbfe28d15144a57bbcc2c9 s 180 d mm r g


A highly experienced WordPress Web Developer, Front-end and Back-end Developer & New Media Specialist with extensive knowledge of a wide spectrum of technologies in the Development and Creative Industries built up over a number of years.

Leave a Reply

Your email address will not be published. Required fields are marked *