Use Current Post ID as a QUERY ARG

Support General Use Current Post ID as a QUERY ARG

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #31827
    Jeremy ParrottJeremy Parrott
    Participant

    I would like to be able to filter by the Parent post id in the query args section of the page builder but none of the post_id variables are working for me. I have tried $post_id and don't know why it won't work. The only thing that does work is when I put an actual post id... see attached.

    https://drive.google.com/file/d/1ybqKxESLI3SS0t8OuhxzaAZAD9rQFZ4z/view?usp=sharing

    #31842
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You should use the code to get the current post ID when registering the meta box and custom fields. The builder does not support doing that.

    Refer to this topic https://support.metabox.io/topic/get-the-post-id-as-value/
    and this documentation https://docs.metabox.io/fields/post/

    #32052
    Jeremy ParrottJeremy Parrott
    Participant

    I still can't get this to work. Here's example code in my functions.php file.

    function monitor_set_controller_number( $meta_boxes ) {
        
        $prefix = '';
        
        $post_id = null;
    // Method #1 - DID NOT WORK ($post_id is NULL)
    //    if ( isset( $_GET['post'] ) ) {
    //  $post_id = intval( $_GET['post'] );
    //    } elseif ( isset( $_POST['post_ID'] ) ) {
    //        $post_id = intval( $_POST['post_ID'] );
    //    }
    
    // Method #2 - DID NOT WORK ($post_id is NULL)
    //    $post_id = $GLOBALS['post']->ID;
    
    // Method #3 - DID NOT WORK ($post_id is NULL)
    //    $post_id = rwmb_meta( $field_id );
    
        $meta_boxes[] = [
            'title'        => __( 'Monitor Fields', 'your-text-domain' ),
            'id'           => 'monitor-fields',
            'post_types'   => ['monitor'],
            'storage_type' => 'custom_table',
            'table'        => 'monitor_table',
            'tabs'         => [
                'monitor_details'      => [
                    'label' => 'Monitor Details',
                    'icon'  => 'chart-bar',
                ],
                'tank_configuration'   => [
                    'label' => 'Tank Configuration',
                    'icon'  => 'category',
                ],
                'local_display_fields' => [
                    'label' => 'Local Display Fields',
                    'icon'  => 'money',
                ],
            ],
            'fields'       => [
                [
                    'name'       => __( 'Location', 'your-text-domain' ),
                    'id'         => $prefix . 'physical_location',
                    'type'       => 'post',
                    'post_type'  => ['location'],
                    'field_type' => 'select',
                    'order by'   => 'post_id',
                    'tab'        => 'monitor_details',
                ],
                [
                    'name'      => __( 'Active', 'your-text-domain' ),
                    'id'        => $prefix . 'monitor_active',
                    'type'      => 'switch',
                    'style'     => 'square',
                    'on_label'  => 'Active',
                    'off_label' => 'Inactive',
                    'std'       => true,
                    'tab'       => 'monitor_details',
                ],
                [
                    'name'       => __( 'Controller', 'your-text-domain' ),
                    'id'         => $prefix . 'controller',
                    'type'       => 'post',
                    'post_type'  => ['controller'],
                    'field_type' => 'select',
                    'query_args' => [
                        'orderby'     => 'post_title',
                        'order'       => 'ASC',
                        'post_parent' => $post_id,
                    ],
                    'tab'        => 'monitor_details',
                ],
                :

    It seems like the post doesn't exist at the time the functions.php is included so it's not defined.

    I know it's reading in the functions.php file because I can change $post_id to 200017 and it brings up the correct controllers.

    #32074
    Long NguyenLong Nguyen
    Moderator

    Hi,

    I think the problem is: the meta box Monitor Fields is assigned to the post type monitor while you are using the field post to show the child posts of the post type controller. Please set the post type correctly and re-check it.

    #32106
    Jeremy ParrottJeremy Parrott
    Participant

    That didn't work.

    I think the code is correct but I did change the post_types from "Monitor" to "Controller" in the first few lines of code to test.

        $meta_boxes[] = [
            'title'        => __( 'Monitor Fields', 'your-text-domain' ),
            'id'           => 'monitor-fields',
            'post_types'   => ['<strong>monitor</strong>'],
            'storage_type' => 'custom_table',
            'table'        => 'monitor_table',

    I left this code "as-is" and it looks correct to me:

                [
                    'name'       => __( 'Controller', 'your-text-domain' ),
                    'id'         => $prefix . 'controller',
                    'type'       => 'post',
                    'post_type'  => ['controller'],
                    'field_type' => 'select',
                    'query_args' => [
                        'orderby'     => 'post_title',
                        'order'       => 'ASC',
                        'post_parent' => $post_id,
                    ],
                    'tab'        => 'monitor_details',

    Here's a screenshot of what I'm trying to do: https://pasteboard.co/hquFYGyQQILC.png

    #32119
    Long NguyenLong Nguyen
    Moderator

    Here is how it works on my end https://share.getcloudapp.com/llukWYRL

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