Skip to content
Advertisement

Custom Endpoint API based on attribute gutenberg block

Viewed 4 times

0

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

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

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

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

        $posts = get_posts($args);

Advertisement

Answer

Based on your code provided, here is a potential solution that may solve your issue:

In your custom block cgb/block-idinheiro-blocks, you could set the value_attribute_my_block attribute source as meta if the block is only used once per a post/page (multiple can be restricted in the attributes – the default is true, set to false). NB. your meta field must be registered with register_post_meta() in php/your main plugin file or the value won’t save.

Storing the value as post_meta enables you to use the powerful meta query to retrieve posts into your function get_items() easily. This strategy avoids having to parse the content of the post, as by default on save block attributes are serialized. In your current query $args, your 's' parameter would find posts with the block but would still have to parse the value of the attribute, which would be an alternative but likely not as fast/performant as meta query would be.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement