• How to display stored value in Meta Box ?

    I added Meta Box with below code.

    
    add_meta_box('wp_news_settings', 'News Settings', 'news_settings_html', 'news', 'normal', 'default');
    

    My call back function is like below

    
    public function news_settings_html($post)
        {
            $wpwi_stored_meta = get_post_meta($post->ID);
            
           
            $fields = [
                'location' => '<input type="text" value="'. (!empty($wpwi_stored_meta) && $wpwi_stored_meta['location']) ? $wpwi_stored_meta['location'][0] : ''.'"  name="location" />',  
            ];
                wp_nonce_field(basename(__FILE__), 'wpwi_nonce');
                
            ?>
                <table class="form-table">
                    <?php
                        foreach($fields as $key=> $field) {
                            ?>
                                <tr valign="top">
                                    <th scope="row">
                                        <label for="location">
                                            <?= _e(ucfirst($key), 'textdomain');?>
                                        </label>
                                    </th>
                                    <td>
                                        <?= $field; ?>
                                    </td>
                                </tr>
                            <?php
                        }
                    ?>
                </table>
            <?php
        }
    

    But I am not getting Stored Value in Meta Box .

Viewing 1 replies (of 1 total)
  • You are not using get_post_meta() correctly. You have only specified the ID here, which gives you all the fields for this one, but you probably only need the “location” field. Better would be:

     $wpwi_stored_meta = get_post_meta($post->ID, 'location', true);
                   
            $fields = [
                'location' => '<input type="text" value="'.$wpwi_stored_meta.'"  name="location" />'  
            ];

    The foreach loop below makes little sense if you have only one field which you store in $fields anyway. You could also simplify it.

    If that doesn’t work either, you can use var_dump() to output what is in $wpwi_stored_meta.

Viewing 1 replies (of 1 total)
  • The topic ‘Display stored value in Meta Box’ is closed to new replies.