Wrong post ID in meta box callback

I trying to get the ID of the post on edit page but its displaying the wrong ID

Custom Post:

register_post_type( 'projetos', array(
    'description'           = 'Projetos do Scan'
    ,'exclude_from_search'  = false
    ,'public'               = true
    ,'publicly_queryable'   = true
    ,'show_ui'              = true
    ,'show_in_menu'         = true
    ,'query_var'            = true
    ,'rewrite'              = array( 'slug' = 'projeto' )
    ,'capability_type'      = 'post'
    ,'has_archive'          = true
    ,'menu_position'        = 20
    ,'supports'             = array( 'title', 'editor' )
    ,'hierarchical'         = true
    ,'labels' = array(
        'name'               = _x( 'Projetos', 'post type general name', 'Projetos do Scan' ),
        'singular_name'      = _x( 'Projeto', 'post type singular name', 'Projetos do Scan' ),
        'menu_name'          = _x( 'Projetos', 'admin menu', 'Projetos do Scan' ),
        'name_admin_bar'     = _x( 'Projeto', 'add new on admin bar', 'Projetos do Scan' ),
        'add_new'            = _x( 'Adicionar Novo', 'foto', 'Projetos do Scan' ),
        'add_new_item'       = __( 'Adicionar Novo Projeto', 'Projetos do Scan' ),
        'new_item'           = __( 'Novo Projeto', 'Projetos do Scan' ),
        'edit_item'          = __( 'Editar Projeto', 'Projetos do Scan' ),
        'view_item'          = __( 'Ver Projeto', 'Projetos do Scan' ),
        'all_items'          = __( 'Todos os Projetos', 'Projetos do Scan' ),
        'search_items'       = __( 'Buscar Projetos', 'Projetos do Scan' ),
        'parent_item_colon'  = __( 'Projetos:', 'Projetos do Scan' ),
        'not_found'          = __( 'Nenhum projeto encontrado.', 'Projetos do Scan' ),
        'not_found_in_trash' = __( 'Nenhum projeto encontrado na lixeira.', 'Projetos do Scan' )
    )
    ,'menu_icon' = 'dashicons-clipboard'
    ,'register_meta_box_cb' = 'projetos_meta_box' // adicionar meta boxes e remover meta boxes neste callback
) );

Callback:

function projetos_meta_box() {
add_meta_box('projetos_info_meta_box', 'Informações', 'render_projetos_info_meta_box', 'projetos', 'side', 'default');
add_meta_box('projetos_imagens_meta_box', 'Imagens', 'render_projetos_imagens_meta_box', 'projetos', 'normal', 'default');
add_meta_box('projetos_capitulos_meta_box', 'Capítulos', 'render_projetos_capitulos_meta_box', 'projetos', 'normal', 'default');

}

and here's the problem

function render_projetos_imagens_meta_box( $post ) {
global $post;

wp_nonce_field( 'projetos_imagens_meta_box', 'projetos_imagens_meta_box');
$imagens_projeto = json_decode( get_post_meta( $post-ID, 'imagens_projeto', true ) );

ob_start(); ?

?php echo var_dump($post); ?
div style="margin-top: 10px;"
    button type="button" id="btn_add_img" class="button button-primary button-large"Adicionar/button
/div
ul class="attachments-projeto"
?php if( !empty( $imagens_projeto ) ) : foreach( $imagens_projeto as $img_pjt ) : ?

    li
        button data-id="?php echo $img_pjt-id; ?" type="button" class="btn_remover_img"
            span class="dashicons dashicons-trash"/span
        /button
        img alt="?php echo $img_pjt-title; ?" src="?php echo $img_pjt-url; ?" /
    /li

?php endforeach; endif; ?
/ul
div class="clearfix"/div
input type="hidden" id="imagens_projeto" name="imagens_projeto" value="?php echo get_post_meta( $post-ID, 'imagens_projeto', true ); ?" /

?php
echo ob_get_clean();

}

Topic metabox custom-post-types Wordpress

Category Web


Since I cannot solve the problem I did a workaround to this

$post_id = intval($_GET['post']);

Basically I getting the post ID from the edit post url


I think your problem come from the json_decode. It's look like the get_post_meta returns you a member data (post_type->membros on the bottom var_dump) !

Have a look on the code to save this meta.


If you remove the line global $post; (and really you should remove it), there is no reason this shouldn't work, according to the official documentation, unless as @rudtek mentioned, you run a custom query somewhere and the $post has changed

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.