-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtaxonomies.php
41 lines (31 loc) · 1005 Bytes
/
taxonomies.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
//LOOP THROUGH TAXONOMIES AND ADD ACF FIELDS TO API
add_action( 'rest_api_init', 'acf2api_hook_all_terms', 99 );
function acf2api_hook_all_terms(){
$taxonomies = array_keys( get_taxonomies() );
foreach ($taxonomies as $taxonomy) {
add_filter( 'rest_prepare_'.$taxonomy, function($data, $term, $request){
//Get the response data
$response_data = $data->get_data();
//Bail early if there's an error
if ( $request['context'] !== 'view' || is_wp_error( $data ) ) {
return $data;
}
//Get all fields
$fields = get_fields('term_'.$term->term_id);
//var_dump($fields);
//If we have fields...
if ($fields){
//Loop through them...
foreach ($fields as $field_name => $value){
//Set the meta
$response_data[$field_name] = $value;
}
}
//Commit the API result var to the API endpoint
$data->set_data( $response_data );
return $data;
}, 10, 3);
}
}
?>