Use Current Post ID as a QUERY ARG
- This topic has 5 replies, 2 voices, and was last updated 3 years, 4 months ago by
Long Nguyen.
-
AuthorPosts
-
November 9, 2021 at 9:40 PM #31827
Jeremy Parrott
ParticipantI 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
November 10, 2021 at 2:49 PM #31842Long Nguyen
ModeratorHi,
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/November 20, 2021 at 12:50 AM #32052Jeremy Parrott
ParticipantI 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.
November 21, 2021 at 5:38 PM #32074Long Nguyen
ModeratorHi,
I think the problem is: the meta box Monitor Fields is assigned to the post type
monitor
while you are using the fieldpost
to show the child posts of the post typecontroller
. Please set the post type correctly and re-check it.November 22, 2021 at 10:42 PM #32106Jeremy Parrott
ParticipantThat 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
November 23, 2021 at 7:00 PM #32119Long Nguyen
ModeratorHere is how it works on my end https://share.getcloudapp.com/llukWYRL
-
AuthorPosts
- You must be logged in to reply to this topic.