Inefficient REST API usage
- Plugins & WordPress
Problem Summary
WordPress REST API can be inefficient when endpoints aren’t optimized—making too many requests, returning excessive data, or lacking caching. This creates unnecessary server load and slower client-side performance.
Performance Impact
- Increased server load: Each API call triggers database queries
- Larger response payloads: Returning more data than needed
- No caching: Repeated requests hit the database every time
- Client-side delays: JavaScript waits for API responses
How to Detect
- Query Monitor plugin: Shows REST API queries and timing
- Chrome DevTools Network: Filter by /wp-json/ to see API calls
- Server logs: High volume of REST API requests
- PageSpeed Insights: May flag slow API responses
Solutions
1. Limit Returned Fields
// Instead of /wp-json/wp/v2/posts// Use ?_fields=id,title,excerpt,featured_media2. Add Caching to Endpoints
<?php
add_action('rest_api_init', function() { header('Cache-Control: public, max-age=3600');});3. Use Transients for Expensive Queries
= get_transient('my_api_data');if (false === ) { = get_posts(['numberposts' => 10]); set_transient('my_api_data', , HOUR_IN_SECONDS);}return ;4. Optimize Query Parameters
// Instead of default per_page=10'posts_per_page' => 5,'orderby' => 'date','order' => 'DESC'5. Disable Unneeded Endpoints
<?php
add_filter('rest_endpoints', function() { unset(['/wp/v2/users']); unset(['/wp/v2/comments']); return ;});