WordPress - cannot get value from metabox in custom post type

I find it difficult to get the value from the metabox in the custom message type.

This is how I register the metabox in a custom post type:

register_post_type( 'poslovi-newsletter',
    array(
         'labels' => array(
         'name' => __( 'Poslovi newsletter' ),
         'hierarchical' => false,
         'singular_name' => __( 'Posalji newsletter' )
        ),
        'public' => true,
        'exclude_from_search' => true,
        'menu_icon' => 'dashicons-email',
        'register_meta_box_cb' => 'add_bez_oznaka_text_metabox'
        )
);

And this is how I handle the display of the metabox on the user type of messages in the control panel, saving data, etc.

function add_bez_oznaka_text_metabox() {
    add_meta_box('poslovi_newsletter_meta', 'Tekst mejla za korisnike bez oznaka', 'bez_oznaka_textarea', 'poslovi-newsletter', 'normal', 'default');   
}
add_action( 'add_meta_boxes', 'add_bez_oznaka_text_metabox' );

function bez_oznaka_textarea( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'poslovi_newsletter_nonce' );
    $poslovi_newsletter_stored_meta = get_post_meta( $post->ID );
    ?>
    <p>
        <label for="meta-textarea" class="poslovi_newsletter-row-title"><?php _e( 'Tekst mejla', 'poslovi_newsletter-textdomain' )?></label> 
        <textarea name="meta-textarea" id="meta-textarea" style="width: 100%; min-height: 200px;"><?php if ( isset ( $poslovi_newsletter_stored_meta['meta-textarea'] ) ) echo $poslovi_newsletter_stored_meta['meta-textarea'][0]; ?></textarea>
    </p>
    <?php
}

function poslovi_newsletter_meta_save( $post_id ) {

    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'poslovi_newsletter_nonce' ] ) && wp_verify_nonce( $_POST[ 'poslovi_newsletter_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

    // Checks for input and saves if needed
    if( isset( $_POST[ 'meta-textarea' ] ) ) {
        update_post_meta( $post_id, 'meta-textarea', $_POST[ 'meta-textarea' ] );
    }

}
add_action( 'save_post', 'poslovi_newsletter_meta_save' );

Now everything is working fine. When I add a new message and enter data, it saves a message with this data. For example, when sending a var_dump message, I see everything, contents, header, date, etc., but I do not see metadata.

Also, when I do this (say, I want to get metadata from a message with id 37422)

$meta_value = get_post_meta( 37422, 'meta-textarea', true );
var_dump($meta_value);

I get vlaue: string (0) ""

, , , , , .

+4
1

, , .

poslovi-newsletter. php :

<?php
$args = array(
    'post_type' => 'poslovi-newsletter',
    'posts_per_page'=>-1,
);
$posts = new WP_Query( $args );
$out = '';
if ($posts->have_posts()){
    while ($posts->have_posts()){
        $posts->the_post();

        $meta = get_post_meta($post->ID);

        $out.= '<div class="test">'.$meta['meta-textarea'][0].'</div>';
    }
}
else{
    echo '<p>' . __('No Posts Found.') . '</p>';
}
?>

<?php echo $out;?>

, , , , meta-textarea, ,

Array (
    [meta-textarea] => Array ( 
                        [0] => Tessst 
                    )
)

"Tessst", , .

, :)

0

Source: https://habr.com/ru/post/1622093/


All Articles