1

I have created a custom post type: "products". In this post type there are six custom fields with information automatically imported from WP All Import.

I would like to retrieve the information from those 6 custom fields using a shortcode. Ideally, it would be possible to retrieve the information by using the slug of the post, so (the "code" being the slug):

[product code="12345678"]

The above should look for the "product" with slug 12345678 and then output the information from those six custom fields.

How should I approach this?

1

Get the Attribute value in the shortcode and get the custom field values:

function product_func($atts) {
    $post_id = $atts['code'];
    $key = "my_custom_field_key";//for 1 field, you can do this 6 times for the 6 values
    $custom_value = get_post_meta($post_id, $key, true);
    return $custom_value;
}

add_shortcode('product', 'product_func');

if you want to debug the post meta field values use this code:

function product_func($atts) {
    $post_id = $atts['code'];
    //lets check if we are getting the att
    echo "<h1>THE ATT, SHOULD MATCH THE CODE YOU SEND</h1></br>";
    echo "<pre>";
    var_dump($post_id);
    echo "</pre>";
    //lets check if we are getting the att
    echo "</br><h1>WE MAKE SURE THE POST IS NOT NULL, MEANING IT SHOULD EXIST</h1></br>";
    $post = get_post( $post_id );
    echo "<pre>";
    var_dump($post);
    echo "</pre>";

    //lets check the meta values for the post
    echo "</br><h1>WE LIST ALL META VALUES TO CHECK THE KEY NAME OF THE CUSTOM FIELD WE WANT TO GET</h1></br>";
    $meta = get_post_meta( $post_id );
    echo "<pre>";
    var_dump($meta);
    echo "</pre>";

    $key = "my_custom_field_key";//for 1 field, you can do this 6 times for the 6 values
    $custom_value = get_post_meta($post_id, $key, true);
    return $custom_value;
}

add_shortcode('product', 'product_func');

it shows each value needed to get the `custom field, should look like this:

enter image description here

so in my case the key would be:

$key = "MY CUSTOM FIELD";
  • Thank you! But now it returns (in your example) "my_custom_field_key". How do I get the value of the custom field to appear? – Wouter Sep 22 '17 at 16:14
  • change the $key to the name of the one of your 6 custom fields, then do a var_dump($custom_value); before the return, show me what that outputs. – David Lee Sep 22 '17 at 16:34
  • Now I have this: 'function productgids_query_product($atts) { $post_id = $atts['ean']; $key = "media_markt_prijs_EUR"; $custom_value = get_post_meta($post_id, $key, true); var_dump($custom_value); return $custom_value; } add_shortcode('product', 'productgids_query_product');' Shortcode is: [product ean="0885909627561"] Now the output is: string(0) "" – Wouter Sep 22 '17 at 17:00
  • i just updated my answer, added code for debugging. – David Lee Sep 22 '17 at 17:59

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.