wordpresswordpress-shortcodepost-meta

get_post_meta shortcode returns empty field


I'm trying to create a custom shortcode that will allow me to input custom order field data into an auto generated outbound email template via the Custom Order Status for WooCommerce plugin.

My understanding of PHP is limited at best but I came up with the below code after hours of searching the forums and Google:

function wcal_abandoned_cart_id_shortcode_callback( $atts ) {
    $atts = shortcode_atts( array(
        'post_id' => get_the_ID(),
    ), $atts, 'wcal_abandoned_cart_id' );

    return get_post_meta( $atts['post_id'], 'wcal_abandoned_cart_id', true );
}
add_shortcode( 'wcal_abandoned_cart_id', 'wcal_abandoned_cart_id_shortcode_callback' );

Wordpress and the plugin seem to recognize the shortcode [wcal_abandoned_cart_id] however the output value is blank. The value that should return for this specific order is "428". I'm hoping someone can help point me in the right direction.

Thanks in advance.


Solution

  • have you tried another way like below?

    global $post;
    
    $meta = get_post_meta( $post->ID, 'wcal_abandoned_cart_id', true );  
    

    please try instead of

    get_post_meta( $atts['post_id'], 'wcal_abandoned_cart_id', true );