From 21643d276c103187f0f03905870cdddd7771ad01 Mon Sep 17 00:00:00 2001 From: Nika Siradze Date: Tue, 30 Jun 2026 16:53:17 +0400 Subject: [PATCH] Rules: enrich post-type field list (ACF + registered meta + standard fields) The If-field / Set-meta-key datalists were sparse because they only read raw DB meta keys. Now /post-type-fields also returns ACF field names, register_post_meta keys, and post_title/post_date, so the rules editor offers a real prefetched list instead of manual typing. --- src/Admin/RestController.php | 44 ++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/Admin/RestController.php b/src/Admin/RestController.php index bc3497b..cc39d56 100644 --- a/src/Admin/RestController.php +++ b/src/Admin/RestController.php @@ -161,18 +161,44 @@ public function post_type_fields( WP_REST_Request $request ): WP_REST_Response { return new WP_REST_Response( [ 'ok' => true, 'fields' => [] ], 200 ); } - $keys = (array) $wpdb->get_col( - $wpdb->prepare( - "SELECT DISTINCT pm.meta_key FROM {$wpdb->postmeta} pm - INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id - WHERE p.post_type = %s AND pm.meta_key NOT LIKE %s - ORDER BY pm.meta_key LIMIT 200", - $type, - $wpdb->esc_like( '_' ) . '%' + $keys = [ 'post_title', 'post_date' ]; + + $keys = array_merge( + $keys, + (array) $wpdb->get_col( + $wpdb->prepare( + "SELECT DISTINCT pm.meta_key FROM {$wpdb->postmeta} pm + INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id + WHERE p.post_type = %s AND pm.meta_key NOT LIKE %s + ORDER BY pm.meta_key LIMIT 300", + $type, + $wpdb->esc_like( '_' ) . '%' + ) ) ); - return new WP_REST_Response( [ 'ok' => true, 'fields' => array_values( array_map( 'strval', $keys ) ) ], 200 ); + // ACF field names for this post type (the values live under the field name as meta). + if ( function_exists( 'acf_get_field_groups' ) && function_exists( 'acf_get_fields' ) ) { + foreach ( (array) acf_get_field_groups( [ 'post_type' => $type ] ) as $group ) { + foreach ( (array) acf_get_fields( $group['key'] ?? '' ) as $field ) { + if ( ! empty( $field['name'] ) ) { + $keys[] = (string) $field['name']; + } + } + } + } + + // Meta registered via register_post_meta(). + foreach ( array_keys( get_registered_meta_keys( 'post', $type ) ) as $registered ) { + if ( strpos( (string) $registered, '_' ) !== 0 ) { + $keys[] = (string) $registered; + } + } + + $keys = array_values( array_unique( array_filter( array_map( 'strval', $keys ) ) ) ); + sort( $keys ); + + return new WP_REST_Response( [ 'ok' => true, 'fields' => $keys ], 200 ); } public function scrape_preview( WP_REST_Request $request ): WP_REST_Response {