Wednesday, August 24, 2022
HomeWordPress Developmentcustomized discipline - How does one correctly use metadata, added with `register_meta();`...

customized discipline – How does one correctly use metadata, added with `register_meta();` inside a metabox callback?


I have been making an attempt to make use of some metadata I registered inside my metabox, however I am hitting just a few snags. I might respect some steerage on the matter because the documentation appears just a little gentle on this specific topic. Customized Metaboxes WPdocs

I am uncertain learn how to accurately use get_post_meta(); inside my callback. At the moment I am utilizing $foo_post_meta = get_post_meta( $post->ID ); inside my callback to get all of the submit meta.(Nice proper?) not precisely, You see I wish to use $foo_post_meta in a foreach loop and outline the fields that ought to be exhibited to the consumer.

Nonetheless, I’ve since come to grasp that customers which might be making a submit for the primary time, (Add New Put up), are receiving an empty array inside $foo_post_meta, this ends in no fields being displayed, and as you’d count on a number of errors.

However to me this in itself is just a little unusual, because the meta fields do exist as a result of I registered them utilizing register_meta(), so why is it a brand new submit returns nothing however an empty array? Whereas, a submit that has been saved renders and shows my looped fields simply effective?

Register the brand new metafields:

// Foo metabox meta fields.
public operate foo_register_post_meta() {
    // Outline submit meta fields.
    $fields = array(
        'field_one',
        'field_two',
        'field_three',
    );

    // Register submit meta fields.
    foreach ( $fields as $discipline ) {
        register_meta( 'submit', $discipline, array(
            'show_in_rest' => true,
            'single' => true,
            'kind' => 'string'
        ) );
    }
}
add_action( 'init', 'foo_register_post_meta' );

The Metabox callback, (Show fields to be used):

// Foo metabox callback.
public operate foo_metabox_callback( $submit ) {
    // Nonce discipline.
    wp_nonce_field( 'foo_metabox', 'foo_metabox_nonce' );

    // Get all submit meta.
    $foo_post_meta = get_post_meta( $post->ID );

    // Past this level, I want to loop '$foo_post_meta' and show fields.
    print_r($foo_post_meta);
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments