$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>metabox - $post->ID incorrect within meta box|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

metabox - $post->ID incorrect within meta box

matteradmin15PV0评论

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);
}

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);
}
Share Improve this question edited Oct 8, 2017 at 16:20 mmm 3,8193 gold badges16 silver badges22 bronze badges asked Oct 8, 2017 at 14:22 Ben HBen H 4036 silver badges20 bronze badges 2
  • is something changing if you comment the call to wp_reset_postdata ? – mmm Commented Oct 8, 2017 at 14:56
  • No with or without it still returns the incorrect post id – Ben H Commented Oct 8, 2017 at 16:20
Add a comment  | 

1 Answer 1

Reset to default 1

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;   

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far