0
  <div class="vp-field vp-checkbox vp-checked-field vp-meta-single" data-vp-type="vp-checkbox" id="_custom_meta[category][]">
<div class="label">
     <label>Categories</label>
</div>
<div class="field">
    <div class="input">
       <label>
          <input class="vp-input" type="checkbox" name="_custom_meta[category][]" value="star">
         <span></span>star</label>
      <label>
       <input class="vp-input" type="checkbox" name="_custom_meta[category][]" value="triangle">
       <span></span>triangle</label>
     <label>
      <input class="vp-input" type="checkbox" name="_custom_meta[category][]" value="square">
      <span></span>square
     </label>
    </div>
</div>
</div>'

I am trying to update the post meta checkboxes, but the below is not working. Any suggestions what i am doing wrong.

 $features[0] = "star";
 $features[1] = "triangle";
 $features[2] = "square";

update_post_meta($post_id, "category",$features); 
0

Your code:

$features[0] = "star";
$features[1] = "triangle";
$features[2] = "square";

update_post_meta($post_id, 'category', $features); 

Is fine, it will only fail if the $post_id is not found. I'm assuming you want to save custom categories, not real WP taxonomies.

Do you have the debug log activated? Does it tell you anything?


Activate debug log.

  1. Download and activate a log viewer.

  2. Open wp-config.php (located in WP root folder), and change the line:

    define( "WP_DEBUG", false );

into

define( "WP_DEBUG", true );// just toggle this line to false to turn off
if ( WP_DEBUG ) {
    define( "WP_DEBUG_DISPLAY", false );
    define( "WP_DEBUG_LOG", true );
    @ini_set( "display_errors", 0 );
    define( "SCRIPT_DEBUG", true );
}

You can now view error logs. Disable logging when you are done!


Working with the debug log
If (for example) you want to check the $post_id that is being used, you can add the following right below the update_post_meta():

error_log('The post id is: '.$post_id);

To check an array i always use:

error_log('The features array is: <pre>'.print_r($features,true).'</pre>');

Regards, Bjorn

  • I know it not the issue with post id, as other text fields are getting updated. where do I check error log? – user8742566 Jun 4 '18 at 1:18
  • follow the 2 steps i mentioned @ Activate debug log. – Bjorn Jun 4 '18 at 1:22
  • yep, I did that. I activated the plugin and edited the wp-config.php – user8742566 Jun 4 '18 at 1:24
  • ok, now you can view the debug log. Go to WP admin --> settings --> debug log. Maybe the log viewer is located somewhere else in the wp admin menu, look for it, it has to be there. I use my own log viewer. – Bjorn Jun 4 '18 at 1:28
  • btw, are you absolutely certain the post_meta is not saved to the database? – Bjorn Jun 4 '18 at 1:43
0

Ok, so probably the meta data is saved correctly, now we are going to retrieve it.

I'm not sure where and how you're adding this code, it looks like a metabox?

The checkboxes aren't magicly getting checked, you need to add some logic to it.

First we need to have a closer look how you save the checkbox data. We want to save to the DB which checkboxes are checked.

This array:

$features = array();
$features[0] = "star";
$features[1] = "triangle";
$features[2] = "square";

Is not going to tell us which categories are checked.

We want to save the array like this:

$features = array();
$features['star'] = 1; // checked
$features['square'] = 0; // not checked
$features['triangle'] = 1; // checked

Above the checkbox HTML you need to get the features data (post_meta) from the DB.

Add this above the html:

<?php
global $post;
$features = get_post_meta( $post->ID, 'category', true );
?>

When you want to check a checkbox you need to add the attribute checked="checked" to it. We only want to do this if the features array from the DB is telling us to do so. You can achieve this with this checkbox html:

<input type="checkbox" name="checkbox_name" id="checkbox_id" value="triangle" <?php echo (isset($features['triangle']) && $features['triangle']) ? 'checked="checked"' : '' ?> />

This:

<?php echo (isset($features['triangle']) && $features['triangle']) ? 'checked="checked"' : '' ?>

Checks if the array key (triangle) is present in the $features array and if the value is set to 1 (or any positive value). If yes, it outputs checked="checked", if not, it does nothing.

Regards, Bjorn

  • I am using vafpress framework(metaboxes). I will not be able to use <?php echo (isset($features['triangle'].....) as suggested because it is in the WP dashboard that I want to see the check marks. I am auto updating the post content using update_post_meta and particular for checkboxes I am not seeing the checkmarks are marked checked in WP dashboard. Rest of the text and text area content gets updated in the WP dashboard. – user8742566 Jun 4 '18 at 11:24
  • Simply said. If you can't add the checked="checked" and vafpress (never heard of it) also does not. Then you're stuck. It has to be one or the other. Contact vafpress to help you further. I can't do anything more for you at this point. – Bjorn Jun 4 '18 at 15:26
  • thanks for all the help. I will dig a little bit more. – user8742566 Jun 4 '18 at 16:30

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.