Query Args Confusion

Support General Query Args ConfusionResolved

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

    Below is the code I am using... it works perfectly if I substitute the actual post_parent ID for the variable $post_parent so I am not sure why it's not working. Do I have the $post_parent variable wrong?

    [
                    'name'       => __( 'Controller', 'your-text-domain' ),
                    'id'         => $prefix . 'controller',
                    'type'       => 'post',
                    'post_type'  => ['controller'],
                    'field_type' => 'select_advanced',
                    'query_args' => [
                'orderby'     => 'post_title',
                            'order'       => 'ASC',
                'post_parent' =>  $post_parent,
                    ],
                    'tab'        => 'monitor_details',
                ],
    #33574
    Long NguyenLong Nguyen
    Moderator

    Hi Jeremy,

    Can you please share the code that gets the post parent? Do you want to set the current post as the post parent?

    #33586
    Jeremy ParrottJeremy Parrott
    Participant

    I don't have that code... I tried this and many other combinations.

    $prefix = '';
    $post_parent = ($_POST['post_parent']);
    $post_id = null;
    if ( isset( $_GET['post'] ) ) {
    $post_id = intval( $_GET['post'] );
    } elseif ( isset( $_POST['post_ID'] ) ) {
    $post_id = intval( $_POST['post_ID'] );
    }

    #33611
    Long NguyenLong Nguyen
    Moderator

    Hi Jeremy,

    So if the current post has child posts, you can assign the variable $post_id to the parameter post_parent

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

    Read more about parameter post_parent here https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

    #33623
    Jeremy ParrottJeremy Parrott
    Participant

    The current post is a child of the same parent that I am wanting to filter by.

    Location = parent
    Controllers = child
    Monitors = child

    My goal is for the query arg to only show the controllers with the same parent as the monitor (current page) I am on.

    #33680
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Ok, I got your case. You can use the WordPress function wp_get_post_parent_id() to get the post parent of the current post. Then use the post parent (ID) in the field query args. For example:

    function your_prefix_function_name( $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'] );
        }
    
        // Get post parent of the current post
        $post_parent = wp_get_post_parent_id( $post_id );
        $meta_boxes[] = [
            ...
            'fields'     => [
                [
                    'name'       => __( 'Controller', 'your-text-domain' ),
                    'id'         => $prefix . 'controller',
                    'type'       => 'post',
                    'post_type'  => ['controller'],
                    'field_type' => 'select_advanced',
                    'query_args' => [
                        'orderby'     => 'post_title',
                        'order'       => 'ASC',
                        'post_parent' =>  $post_parent, // Show child posts
                        'post__not_in' => [ $post_id ] // Exclude current post
                    ],
                    'tab'        => 'monitor_details',
                ],
            ],
        ];
        return $meta_boxes;
    }

    Read more on the documentation https://developer.wordpress.org/reference/functions/wp_get_post_parent_id/

    #33776
    Jeremy ParrottJeremy Parrott
    Participant

    I figured it out... thank you. But for some reason, I had to replace this code block:

    $prefix = '';

    $post_id = null;
    if ( isset( $_GET['post'] ) ) {
    $post_id = intval( $_GET['post'] );
    } elseif ( isset( $_POST['post_ID'] ) ) {
    $post_id = intval( $_POST['post_ID'] );
    }
    

    with this:

    $prefix = '';

    $post_id = null;
    $parts = parse_url($_SERVER['REQUEST_URI']);
    $pieces = explode('/', $parts['path']);
    $post_id = $pieces[2];
    
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.