Custom Fields
Re-Envisioned
Support MB User Profile Get the post id as value

  • Creator
    Topic
  • #24046
    Resolved ben06
    Participant

    I created a meta box and I need to get the id of the current post as a value with the parameter 'attributes' :

    array(
      'id'   => $prefix . 'hidden_postid',
      'type' => 'hidden',
      'attributes' => array(
           'value' =>  $post_id,
       )
    ),

    First I used this :

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

    But it doesn't seem to work.

    The function get_the_ID() doesn't work either however I use the 'value' => date() for another field and it's working...

    Thank you 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • Author
    Replies
  • #24051
    Long Nguyen
    Moderator

    Hi,

    You can use this code to get the post ID when registering the meta box

    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $prefix = '';
        $post_id = null;
        if ( isset( $_GET['post'] ) ) {
        $post_id = intval( $_GET['post'] );
        } elseif ( isset( $_POST['post_ID'] ) ) {
        $post_id = intval( $_POST['post_ID'] );
        }
    
        $meta_boxes[] = [
            ...
            'fields'     => [
                [
                    'id'         => $prefix . 'field_id',
                    'type'       => 'text',
                    'name'       => esc_html__( 'Text Field', 'text-domain' ),
                    'attributes' => [
                        'value' => $post_id
                    ],
                ],
            ],
        ];
        return $meta_boxes;
    }

    Refer to this topic https://metabox.io/support/topic/dynamic-created-metaboxes-for-specific-pages-are-not-saving-using-only-code/.

    But this feature does not support the field hidden, the following field is supported text, url, email, checkbox, radio, date, time, datetime, number. Get more details in the documentation https://docs.metabox.io/custom-attributes/.

    #24072
    ben06
    Participant

    Hi,

    Thank you for the answer.

    But this is not working...
    I'm using a plugin (Betterdocs), maybe that is the reason why it is not working.

    The URL is xxxxxx.com/docs/doc-1/

    But when I try to echo the post ID on the layout page of the plugin, it is working well.

    Any other ideas ?

    #24073
    Long Nguyen
    Moderator

    Hi,

    Get and echo the post ID on the front end - template of the plugin xxxxxx.com/docs/doc-1/ - is different from the backend when registering the meta box and custom fields.

    Could you please share some screenshots (full screen) and the code that creates the meta box, custom fields where the post ID is not retrieved?

    #24074
    ben06
    Participant

    Here is the code that creates the metabox :

    https://pastebin.com/wmc5ufqB

    #24077
    Long Nguyen
    Moderator

    Hi,

    Have you tried the code that I post above?

    function confirmation_lecture_register_meta_boxes( $meta_boxes ) {
        $prefix = '';
        // Get the $post_id here
        $post_id = null;
        if ( isset( $_GET['post'] ) ) {
            $post_id = intval( $_GET['post'] );
        } elseif ( isset( $_POST['post_ID'] ) ) {
            $post_id = intval( $_POST['post_ID'] );
        }
    
        $meta_boxes[] = [
            'title'      => esc_html__( 'Confirmation de lecture', 'text-domain' ),
            'id'         => 'confirmation-lecture',
            'fields'     => [
                [
                    'id'   => $prefix . 'testtest',
                    'type' => 'text',
                    'attributes' => array(
                        'value' =>  $post_id, // Here is the value who's not working
                    )
                ]
            ],
        ];
    
        return $meta_boxes;
    }
    #24084
    ben06
    Participant

    Hi,

    Yes I tried the code but it still not working...

    How is that possible?

    #24085
    ben06
    Participant

    Hi,

    Yes I tried the code but it still not working...

    I also tried this :

    $test = $GLOBALS['post']->ID;

    The variable retrieves the good post id but it's not working as a value...

    #24086
    Long Nguyen
    Moderator

    Hi,

    Please see my screen record https://share.getcloudapp.com/DOuopnDR. The code works as well on my local site. If it still does not work, please share the admin site account via this contact form, I will check it out.

    #24087
    ben06
    Participant

    Thank you for the screen record.

    It's working well on back end but I need to get the post id when I submit the front end form trough a shortcode.

    As I said, i can't get the post id from $_GET because I use a plugin and the url is modified.
    I think I have to retrieve the post id with a global.

    #24091
    Long Nguyen
    Moderator

    Hi,

    If you are using the extension MB Frontend Submission to submit a post from the frontend, you can use the action hook rwmb_frontend_after_process to update the current post ID to the field value.

    The code would be:

    add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
        if ( 'confirmation-lecture' === $config['id'] ) {
            update_post_meta( $post_id, 'testtest', get_queried_object_id() );
        }
    }, 10, 2 );

    $post_id is the ID of the post that will be created.
    get_queried_object_id() is the function which returns the current post ID (the post that shows the form submit).

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.