0

In my custom post-type, I am building a multidimensional array meta-box with multiple inputs, with below code:

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

    foreach ((array) $services as $service) {
        echo '<div class="inside">
        <div>
            <label>Title</label>
            <input type="text" name="service[][title]" value="' . $service['title'] . '">
        </div>
        <div>
            <label>Type</label>
         <input type="text" name="service[][type]" value="' . $service['type'] . '">
        </div>
        <div>
            <label>Content</label>
            <textarea name="service[][text]">' . $service['text'] . '</textarea>
        </div>';
    }
    exit;
    ?>

It is throwing the Warning:

Illegal string offset 'title' and same warning for 'type' & 'text'.

I have tried double-quotes as well also.

Using the below code to store the data, if it helps:

function service_save_meta_box_data($post_id) {
// store custom fields values
if (isset($_REQUEST['services'])) {
 update_post_meta($post_id, 'services', sanitize_text_field($_POST['service']));
}
}
add_action('save_post_service', 'service_save_meta_box_data');
theKing
  • 714
  • 3
  • 14
  • 36
  • Just make var_dump($service); after foreach() line to see what the problem is. – Elvin Haci Jun 17 '19 at 07:09
  • `string '' (length=0)` – theKing Jun 17 '19 at 07:14
  • Issue is `foreach ((array) $services as $service)`, Only in `foreach` loop I am getting this warning. – theKing Jun 17 '19 at 07:47
  • it means the problem is in saving process. remove sanitize_text_field from update_post_meta($post_id, 'services', sanitize_text_field($_POST['service']));. and test it again. if it works, then apply sanitize_text_field to array elements by using array_map() function – Elvin Haci Jun 17 '19 at 09:49

0 Answers0