Add Post ID to Custom Field And Then to Admin Column

Support MB Admin Columns Add Post ID to Custom Field And Then to Admin ColumnResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #31704
    JanosJanos
    Participant

    Hello,

    I would like to display the post id in a custom field, so later I can display it as an admin column. Could you please help me on how to do it?

    Thank you!

    #31709
    Long NguyenLong Nguyen
    Moderator

    Hi Janos,

    You can create a text field, enable show in admin columns, then use the action hook rwmb_{$field_id}_after_save_field to update the field value by post ID. For example:

    add_action( 'rwmb_fieldID_after_save_field', function ( $null, $field, $new, $old, $post_id ) {
        $storage = $field['storage'];
        $storage->update( $post_id, $field['id'], $post_id );
    }, 10, 5 );

    Refer to this documentation https://docs.metabox.io/actions/#rwmb_after_save_field

    #31712
    JanosJanos
    Participant

    Thank you, Long!

    So, if my field ID is pro_new_id and the field label is ID, then the code will look like:

    add_action( 'rwmb_pro_new_id_after_save_field', function ( $null, $field, $new, $old, $post_id ) {
        $storage = $field['ID'];
        $storage->update( $post_id, $field['pro_new_id'], $post_id );
    }, 10, 5 );

    Is that correct? Does it matter that I made the field Read-Only? Sorry, I barely speak PHP.

    Thank you!

    #31717
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Just replace fieldID by your field ID pro_new_id

    add_action( 'rwmb_pro_new_id_after_save_field', function ( $null, $field, $new, $old, $post_id ) {
        $storage = $field['storage'];
        $storage->update( $post_id, $field['id'], $post_id );
    }, 10, 5 );
    #31722
    JanosJanos
    Participant

    Working perfectly, thank you very much!

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