Query Args Confusion
- This topic has 6 replies, 2 voices, and was last updated 3 years, 2 months ago by
Jeremy Parrott.
-
AuthorPosts
-
January 29, 2022 at 7:00 AM #33555
Jeremy Parrott
ParticipantBelow 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', ],
January 30, 2022 at 5:55 PM #33574Long Nguyen
ModeratorHi Jeremy,
Can you please share the code that gets the post parent? Do you want to set the current post as the post parent?
February 1, 2022 at 1:38 AM #33586Jeremy Parrott
ParticipantI 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'] );
}February 2, 2022 at 2:47 PM #33611Long Nguyen
ModeratorHi Jeremy,
So if the current post has child posts, you can assign the variable
$post_id
to the parameterpost_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-parametersFebruary 2, 2022 at 10:30 PM #33623Jeremy Parrott
ParticipantThe current post is a child of the same parent that I am wanting to filter by.
Location = parent
Controllers = child
Monitors = childMy goal is for the query arg to only show the controllers with the same parent as the monitor (current page) I am on.
February 6, 2022 at 10:40 PM #33680Long Nguyen
ModeratorHi,
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/
February 9, 2022 at 1:03 AM #33776Jeremy Parrott
ParticipantI 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];
-
AuthorPosts
- You must be logged in to reply to this topic.