0

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

Wrong post 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();

}

4
  • try to remove global $post; at the beginning
    – mmm
    Commented Oct 13, 2017 at 16:14
  • It doesn't work :/ I already did it Commented Oct 13, 2017 at 17:35
  • are you using any custom queries anywhere else?
    – rudtek
    Commented Oct 13, 2017 at 19:06
  • nope, there is no custom query at all Commented Oct 14, 2017 at 3:02

3 Answers 3

1

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

0

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.

-1

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

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.