Get the POST ID when defining a metabox

Support General Get the POST ID when defining a metaboxResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #18542
    maybeadevmaybeadev
    Participant

    Hi:

    In core WP, the filter for defining a metabox gives us the POST ID so we can use data from the post to determine whether or not to even paint the metabox.

    In METABOX.IO I can't seem to find a way to get to the POST data while defining the metabox. I want to look at some field values on the post to determine whether or not to even show the entire metabox in the first place.

    Can someone point me to how that might be accomplished? I've looked at the conditional add-ons but they don't seem to do the trick as far as I can tell.

    Thanks.

    #18574
    Anh TranAnh Tran
    Keymaster

    Hi,

    You can get the post ID with the following code:

    $post_id = null;
    if ( isset( $_GET['post'] ) ) {
        $post_id = intval( $_GET['post'] );
    } elseif ( isset( $_POST['post_ID'] ) ) {
        $post_id = intval( $_POST['post_ID'] );
    }
    #45123
    Oleaw DigitalOleaw Digital
    Participant

    Hello,

    I'm currently working on generating dynamic field values for use in a Query Loop. I've developed a plugin to add properties to the IQQR post type using a hidden field. I'm utilizing Greenshift's Query Loop Builder, which allows access to Custom Fields. While I can see the iqqr_qr_url custom field, it's returning $post_id as NULL, and I'm only receiving "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=". Any suggestions how I can achieve this. Please see the attached code.

    <div>
    <?php
    /*
    Plugin Name: IQQR Properties
    Plugin URI: https://iqqr.info/
    Description: This is my custom plugin that adds a dynamic property field to a custom meta box.
    Version: 1.0
    Author: Ava Carter
    Author URI: https://iqqr.info/
    */

    add_filter( 'rwmb_meta_boxes', 'iqqr_properties' );

    function iqqr_properties( $meta_boxes ) {
    $prefix = '';
    $post_id = get_the_ID();

    $value = 'https://api.qrserver.com/v1/create-qr-code/?size=150x150&data='.$post_id;

    $meta_boxes[] = [
    'title' => __( 'IQQR Properties', 'iqqr-info' ),
    'post_types' => ['iqqr'],
    'fields' => [
    [
    'id' => $prefix . 'iqqr_qr_url',
    'type' => 'hidden',
    'std' => $value,
    ],
    ],
    ];

    return $meta_boxes;
    }

    </div>

    Thanks

    #45124
    Oleaw DigitalOleaw Digital
    Participant

    I have also tried this code. The results are same.

    <?php
    /*
    Plugin Name: IQQR Properties
    Plugin URI: https://iqqr.info/
    Description: This is my custom plugin that adds a dynamic property field to a custom meta box.
    Version: 1.0
    Author: Ava Carter
    Author URI: https://iqqr.info/
    */
    
    add_filter( 'rwmb_meta_boxes', 'iqqr_properties' );
    
    function iqqr_properties( $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'] );
        }
    
        $value = 'https://api.qrserver.com/v1/create-qr-code/?size=150x150&data='.$post_id;
    
        $meta_boxes[] = [
            'title'      => __( 'IQQR Properties', 'iqqr-info' ),
            'post_types' => ['iqqr'],
            'fields'     => [
                [
                    'id'   => $prefix . 'iqqr_qr_url',
                    'type' => 'hidden',
                    'std'  => $value,
                ],
            ],
        ];
    
        return $meta_boxes;
    }
    
    #45125
    PeterPeter
    Moderator

    Hello,

    When creating a new post, before publishing it, the post ID will not be created so the code to get the post ID doesn't work.
    You can update the field value after publishing the post with the hook save_post.

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