0

I have a meta box and I am trying to get the current post id however I am getting the ID of a different post. Is there another way to get the ID?

function global_notice_meta_box() {

    $screens = array( 'avent_event' );

    foreach ( $screens as $screen ) {
        add_meta_box(
            'global-notice',
            __( 'Event Attendees', 'sitepoint' ),
            'global_notice_meta_box_callback',
            $screen
        );
    }
}

add_action( 'add_meta_boxes', 'global_notice_meta_box' );


function global_notice_meta_box_callback( $post ) {

    wp_reset_postdata();

    // 1st Method - Declaring $wpdb as global and using it to execute an SQL query statement that returns a PHP object
    global $wpdb;
    $results = $wpdb->get_results( 'SELECT * FROM avent_event_guestlist WHERE event_id = '.$post->ID, OBJECT );

    print_r($post);
}
  • is something changing if you comment the call to wp_reset_postdata ? – mmm Oct 8 '17 at 14:56
  • No with or without it still returns the incorrect post id – Ben H Oct 8 '17 at 16:20
0

Use the global variable $post. It is not specific to the Metabox, which is really just a portal for display.

 global $post;
 $PostId     = $post->ID;   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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