Support

Account

Home Forums ACF PRO 502 Bad Gateway with ACF PRO – WP REST API

Solved

502 Bad Gateway with ACF PRO – WP REST API

  • 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. The strange thing is this works, until I add more than 3 blocks. Anymore than 3 blocks it throws this error 502 Bad Gateway

    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',
      ));
    });
  • For anyone who is interested I found a solution. I extract this code into a Controller class that extends WP_REST_Controller I then set up three methods inside of that controller; register_routes, get_fields and handle. Inside of the register_routes method I simply set up the following;

    register_rest_route( getenv('API_BASE') . '/v' . getenv('API_VERSION'), 'post', array(
          'methods' => 'GET',
          'callback' => array($this, 'handle'),
        ));

    Inside the get_fields method I set up a simple handle to get the field by passing in a specific block;

      public function get_fields($block) {
        acf_setup_meta( $block['attrs']['data'], $block['attrs']['id'], true );
          return get_fields();
        acf_reset_meta( $block['attrs']['id'] );
      }

    From there the rest of the code stayed pretty much the same inside of the handle method I just changed $blocks[$key]['data'] = $fields; to $blocks[$key]['data'] = $this->get_fields($block);

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.