Custom Fields
Re-Envisioned
Support MB Admin Columns Add Post ID to Custom Field And Then to Admin Column

  • Creator
    Topic
  • #31704
    Resolved Janos
    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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #31709
    Long 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
    Janos
    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 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
    Janos
    Participant

    Working perfectly, thank you very much!

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