0

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 Meta Box Value.

1 Answer 1

1

Firstly,

you have to pass get_post_meta() field name in your input type name which you have to store user data value and if it's not created then it will create in save_your_fields_metabox() function.

public function news_settings_html($post){

global $post;
$meta = get_post_meta($post->ID, 'your_store_field_name', true);

<input type="hidden" name="your_meta_box_nonce" value="<?php echo wp_create_nonce(basename(__FILE__)); ?>">

<input type="text" value="<?php
        if (is_array($meta) && isset($meta['text-address'])) {
            echo $meta['text-address'];
        }
        ?>" name="your_store_field_name[text-address]" />
}

you have to store your textbox value in database,then you can get them on front page.

function save_your_fields_metabox($post_id) {

    // verify nonce
    if (isset($_POST['your_meta_box_nonce']) && !wp_verify_nonce($_POST['your_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }

    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

   // check permissions
    if (isset($_POST['post_type'])) { 
        if ('careers' === $_POST['post_type']) {
            if (!current_user_can('edit_page', $post_id)) {
                return $post_id;
            } elseif (!current_user_can('edit_post', $post_id)) {
                return $post_id;
            }
        }
    }

    // if field is not created early then it will create here!
    $old = get_post_meta($post_id, 'your_store_field_name', true);
        if (isset($_POST['your_store_field_name'])) { 
            $new = $_POST['your_store_field_name'];
            if ($new && $new !== $old) {
                update_post_meta($post_id, 'your_store_field_name', $new);
            } elseif ('' === $new && $old) {
                delete_post_meta($post_id, 'your_store_field_name', $old);
            }
        }
}

add_action('save_post', 'save_your_fields_metabox');

and on front page, suppose it's page.php , just you have to get meta field name and get with it's input text name.

 $meta = get_post_meta($post->ID, 'your_store_field_name', true);
    <h1>Text Input</h1>
    <?php echo $meta['text-address']; ?>

Please try to get data from post meta, if it's really stored or not. you can refer this, in detail Metabox

<?php
    $args = array('post_type' => 'news');
    $the_query = new WP_Query($args);

    while ( $the_query->have_posts() ) : $the_query->next_post();
    $id= $the_query->post->ID;
    $location = get_post_meta($id, 'your_store_field_name', true);
    echo $location;
    endwhile;
?>
3
  • Thanks @Akshail Tejani. Actually I would like to pass value through Array and I would like to get output using Loop. Could you please help me in this way ? Thanks.
    – Foysal
    Jul 9, 2022 at 8:18
  • 1
    Okay, Sure @Foysal Jul 9, 2022 at 9:05
  • 1
    I added 2nd point , please try that @Foysal Jul 9, 2022 at 9:20

Your Answer

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

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