0

I am using get_post_meta() to retrieve a custom meta field value from a given post.

The custom meta box has a parent checkbox which has 3 child checkboxes. I want to get the values of both parent and child.

  • Parent checkbox Enable Protection for this post

  • Child checkboxes: (a) Level 1 (b) Level 2 (c) Level 3

here is the screenshot of the metabox. https://www.screencast.com/t/7fA19sCoe2yD

The code I am using is:

$meta = get_post_meta( '8142', '_key_value', true );
foreach( $meta as $m ){
  echo $m['protect']; 
  echo $m['selectedlevels'];
}

But it only echo the $m['protect'] which is also the parent checkbox. How can I output its child checkboxes too?

if I var_dump, it returns the following value:

array(2) { ["protect"]=> bool(true) ["selectedlevels"]=> array(1) { [0]=> int(2) } }

Any help would be highly appreciated.

thanks in advance :)

2
  • I'm not sure I understand what you're asking. Not all the checkboxes are going to be saved, only the selected checkboxes will be saved, as your var_dump() shows.
    – Howdy_McGee
    Apr 4, 2017 at 17:12
  • sorry about the confusion. Yes you are right. I want to get the values of only checked ones.
    – bakar
    Apr 4, 2017 at 17:13

1 Answer 1

2

Based on your var_dump() I can only assume the checked values are in the selecteclevels array which you can get by looping through the array:

foreach( $meta['selectedlevels'] as $level ) {
    echo $level;
}

Or using array indices: echo $meta['selectedlevels'][0]. Note, get_post_meta() may return nothing, and you should check if the selectedlevels index even exists your returned variable.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.