Get the post id as value
Support › MB User Profile › Get the post id as valueResolved
- This topic has 10 replies, 2 voices, and was last updated 4 years, 3 months ago by
Long Nguyen.
-
AuthorPosts
-
January 15, 2021 at 10:56 PM #24046
ben06
ParticipantI 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 🙂
January 16, 2021 at 10:52 AM #24051Long Nguyen
ModeratorHi,
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://support.metabox.io/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 supportedtext
,url
,email
,checkbox
,radio
,date
,time
,datetime
,number
. Get more details in the documentation https://docs.metabox.io/custom-attributes/.January 17, 2021 at 8:32 PM #24072ben06
ParticipantHi,
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 ?
January 17, 2021 at 10:18 PM #24073Long Nguyen
ModeratorHi,
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?
January 17, 2021 at 10:27 PM #24074ben06
ParticipantHere is the code that creates the metabox :
January 18, 2021 at 9:25 AM #24077Long Nguyen
ModeratorHi,
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; }
January 18, 2021 at 3:58 PM #24084ben06
ParticipantHi,
Yes I tried the code but it still not working...
How is that possible?
January 18, 2021 at 4:14 PM #24085ben06
ParticipantHi,
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...
January 18, 2021 at 6:00 PM #24086Long Nguyen
ModeratorHi,
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.
January 18, 2021 at 6:10 PM #24087ben06
ParticipantThank 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.January 19, 2021 at 12:13 PM #24091Long Nguyen
ModeratorHi,
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). -
AuthorPosts
- You must be logged in to reply to this topic.