
Hello there,
I’ve come across a really strange issue. I am using ACF Blocks to build into my WordPress API. I am using WordPress as a headless CMS with a React.js front-end. Although, I am having a problem.
Here is my current set up;
function post_endpoint( WP_REST_Request $request ) {
$slug = $request->get_query_params()['slug'] ?: null;
if( $slug ) {
$args = array(
'name' => $slug,
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 1
);
$post = get_posts($args)[0];
$blocks = parse_blocks($post->post_content);
$collect = array();
// Loop through the blocks
foreach($blocks as $key => $block){
//Setup global block post data context
acf_setup_meta( $block['attrs']['data'], $block['attrs']['id'], true );
// Get ACF fields
$fields = get_fields();
$blocks[$key]['name'] = $block['blockName'];
$blocks[$key]['data'] = $block;
if($blocks[$key]['name'] == null) {
unset($blocks[$key]);
}
// Collection of fields using the block id.
if($block['attrs']['id']) {
$blocks[$key]['attrs'] = [];
$blocks[$key]['data'] = $fields;
}
// Restore global context
acf_reset_meta( $block['attrs']['id'] );
}
$post->blocks = array_values($blocks);
$response = new Article($post->post_title, get_the_date('dS F Y', $post->ID), $post->post_name, $post->blocks);
if( $response ) :
return $response;
endif;
}
return false;
}
add_action( 'rest_api_init', function () {
register_rest_route( API_BASE.'/v'.API_VERSION, 'post', array(
'methods' => 'GET',
'callback' => 'post_endpoint',
));
});
The strange thing is this works, until I add more than 3 blocks. Anymore than 3 blocks it throws this error 502 Bad Gateway