Ecommerce Shopify WordPress Discussion

Custom endpoint to retrieve comments from posts if user is not logged in: not working

Situation: WordPress by default does not allow access to comments of a post via REST API if user is not logged in I tried by passing this by creating a custom endpoint Custom endpoint shows a list of all comments with associated post IDs Problem: I can't access a specific comment for a certain post ID (or a list of all the comments for let's say post ID = 5. How can I fix this? My code in functions.php: function custom_get_comments_by_post_id($request) { $post_id = $request->get_param('id'); // Retrieve the post ID from the request parameter global $wpdb; $comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = 1", $post_id ) ); if (empty($comments)) { return new WP_REST_Response(array(), 200); // Return an empty array if no comments found } return new WP_REST_Response($comments, 200); } function register_custom_endpoints() { register_rest_route('custom/v1', '/post-comments/(?P<id>\d+)', array( 'methods' => 'GET', 'callback' => 'custom_get_comments_by_post_id', 'permission_callback' => '__return_true', // Allow access to all users )); } add_action('rest_api_init', 'register_custom_endpoints');
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.