Created
September 24, 2024 05:07
-
-
Save shishirraven/9d13bf1c7ab84e67fe708fa8c2220dcc to your computer and use it in GitHub Desktop.
Why are we running a loop of CPT twice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('rest_api_init', function () { | |
// Path to the config file | |
$config_path = plugin_dir_path(__FILE__) . '../admin_pages/site.json'; | |
$config = json_decode(file_get_contents($config_path), true); | |
$post_types = get_post_types(array('public' => true, '_builtin' => false), 'objects'); | |
$order_fields = array(); | |
// Collect order fields from CPT config | |
foreach ($config as $cpt) { | |
if (isset($cpt['fields'])) { | |
foreach ($cpt['fields'] as $field) { | |
if ($field['type'] === 'order') { | |
$order_fields[$field['id']] = $field['id']; | |
} | |
} | |
} | |
} | |
// Register a REST route for each CPT | |
foreach ($post_types as $post_type) { | |
$type_slug = $post_type->rest_base ? $post_type->rest_base : $post_type->name; | |
foreach ($order_fields as $field_id) { | |
// Register a REST route for each order field | |
register_rest_route('wp/v2', "/{$type_slug}_{$field_id}/", array( | |
'methods' => 'POST', | |
'callback' => function (WP_REST_Request $request) use ($field_id) { | |
return update_post_order_with_meta($request, $field_id); | |
}, | |
'args' => array( | |
'itemsToOrder' => array( | |
'required' => true, | |
'validate_callback' => function ($param, $request, $key) { | |
return is_array($param); | |
}, | |
), | |
'parent' => array( | |
'required' => false, | |
), | |
), | |
)); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment