-1

I'm using woocommerce product bundles extension. I need to manipulate some informations about bundled post. Actually I found the bundled product container has a hidden custom field named _bundled_ids which in the database has this value a:3:{i:0;i:105;i:1;i:80;i:2;i:70;}

For what I understand this value is the list of the ids of the products bundled.

How can I extract this infos in a comma separated list of ids (i.e. 105,80,70)?

I have tried with get_custom_post_meta() but everything I can get is ARRAY.

Hope you can help. Best regards and thanks

Carlo

EDIT

As I already stated I need to get the data stored in this custom field as a comma separated list of numbers.

I tried to create a shortcode with this code inside my functions.php file:

add_shortcode('bundled_ids','bundled_ids_func');
function bundled_ids_func() {    
    $meta_values = get_post_meta( $post->ID , '_bundled_ids' );    
    $mydata = unserialize($meta_values);

    return $mydata;
}

But I get nothing

EDIT 2

add_shortcode('bundled_ids','bundled_ids_func');
function bundled_ids_func() {
    $meta_values = get_post_meta( get_the_ID() , '_bundled_ids', true );
    $id_list = print_r($meta_values, true);

    return $id_list;
}

with this code I get this result:

Array ( [0] => 105 [1] => 80 [2] => 70 )

  • 1
    That is a serialized array, which is why you get an array when you use get_custom_meta. That function will unserialize if needed. It is unclear what you are asking. Please try to clarify. – s_ha_dum Jan 11 '14 at 15:06
  • Hi @s_ha_dum, thanks for your comment. I'm trying to get a list of values stored in the array separated by comma. – carletto0282 Jan 11 '14 at 16:49
1

This is mostly pure PHP, but you need to implode the array.

add_shortcode('bundled_ids','bundled_ids_func');
function bundled_ids_func() {    
    $meta_values = get_post_meta( get_the_ID() , '_bundled_ids', true );  
    if (!empty($meta_values)) {
      $mydata = implode(',',$meta_values); // add this line
      return $mydata;
    }
}
  • Hi @s_ha_dum with this code I get nothing. :( – carletto0282 Jan 11 '14 at 17:37
  • 1
    Sorry, you need the third parameter for get_post_meta set to true. – s_ha_dum Jan 11 '14 at 17:40
  • Even setting the third parameter to true I get nothing. Maybe setting a foreach could help? – carletto0282 Jan 11 '14 at 17:46
  • @Carletto0282 : see the edit. That is what I get for trying to post code without running it. – s_ha_dum Jan 11 '14 at 17:54
  • Great man! thank you very much! I was trying something like add_shortcode('bundled_ids','bundled_ids_func'); function bundled_ids_func() { $meta_values = get_post_meta( get_the_ID() , '_bundled_ids', true ); foreach ($meta_values as $key => $value) { $ids .= $value.', '; } return $ids; } but yuor code is far way better! Thanks again – carletto0282 Jan 11 '14 at 18:02

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.