[Solved] Custom Endpoint API based on attribute gutenberg block

Renan Bessa Asks: Custom Endpoint API based on attribute gutenberg block
I need to return all posts that have the value of a specific attribute that is saved in the custom block attributes gutenberg. This value in the query will be according to the endpoint below.

http://idinheiro.local/wp-json/idinheiro/v1/blocks-posts/id-here-attribute-gutenberg-block

below my callback function. In short, how do I look for this attribute and put it there in get_posts?

Register routers

Code:
public function register_routes() {
        $namespace = 'idinheiro/v1';
        $path = 'blocks-posts';

        register_rest_route( $namespace, '/' . $path . '/(?P<id>[A-Za-z0-9_-]+)', [
            [
                'methods'             => 'GET',
                'callback'            => [$this, 'get_items'],
                'permission_callback' => [$this, 'get_items_permissions_check'],
            ],
        ]); 
    }

Response data

Code:
public function get_items($request) {
        $args = [
            'post_status' => 'publish',
            'post_type' => 'post',
            's' => 'cgb/block-idinheiro-blocks',
        ];

        $posts = get_posts($args);

        if (empty($posts)) {
            return new WP_Error( 'empty_post', 'there is no blocks inside on posts', [ 'status' => 404 ] );
        }

        $data = [];
        $i = 0;

        foreach ($posts as $post) {
            $data[$i]['id'] = $request['id'];
            $data[$i]['post_id'] = $post->ID;
            $data[$i]['post_name'] = $post->post_title;
            $data[$i]['post_url'] = get_permalink($post->ID);
            $i++;
        }

        wp_reset_postdata();

        return new WP_REST_Response($data, 200);
    }

I thought of something that went something like this. Or if you have another solution on how to get this, I’ll be grateful

Code:
$args = [
            'post_status' => 'publish',
            'post_type' => 'post',
            's' => 'cgb/block-idinheiro-blocks',
            'value_attribute_my_block' = $request['id']
        ];

        $posts = get_posts($args);

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.