Post Views Counter exposes a REST API that enables view tracking and data retrieval for posts, terms, users, and custom objects. The REST API serves two primary purposes:
- View Recording — Endpoints like
view-post,view-term,view-user, andview-otherreceive view events from the frontend when using JavaScript Mode or REST API Mode for counting - Data Retrieval — Endpoints like
get-post-views,post-views,site-views, and similar routes allow fetching view counts for display or integration with external systems
All routes share the canonical namespace post-views-counter. The base URL follows the WordPress REST API pattern:
https://example.com/wp-json/post-views-counter/
API Reference
Namespace
All endpoints use the namespace post-views-counter.
post-views-counter
This namespace is registered with WordPress REST API infrastructure and all routes are prefixed with /wp-json/post-views-counter/.
Endpoints Available in Lite
update-post-views
Records post views from the block editor interface.
| Property | Value |
|---|---|
| Route | /wp-json/post-views-counter/update-post-views/ |
| Methods | POST |
| Callback | block_editor_update_callback |
| Permission Callback | check_rest_route_permissions |
| Source File | includes/class-admin.php |
Parameters:
| Name | Type | Description |
|---|---|---|
id |
integer | The post ID to update |
sanitize_callback |
callable | Sanitization function for the request |
This endpoint is used internally by the WordPress block editor to update view counts during editing sessions. It requires authentication with appropriate capabilities.
view-post
Records a view event for a specific post. Used by JavaScript Mode and REST API Mode counting.
| Property | Value |
|———-|——-|
| Route | /wp-json/post-views-counter/view-post/(?P<id>\d+)|/view-post/ |
| Methods | POST |
| Callback | check_post_rest_api |
| Permission Callback | view_post_permissions_check |
| Source File | includes/class-counter.php |
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
id |
integer | Yes | The post ID to record a view for |
The route pattern allows both /view-post/123 and /view-post/ formats. When JavaScript Mode or REST API Mode is active, the plugin’s frontend JavaScript automatically sends POST requests to this endpoint.
get-post-views
Retrieves view counts for one or more posts.
| Property | Value |
|---|---|
| Route | /wp-json/post-views-counter/get-post-views/(?P<id>(\d+,?)+) |
| Methods | GET, POST |
| Callback | get_post_views_rest_api |
| Permission Callback | get_post_views_permissions_check |
| Source File | includes/class-counter.php |
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
id |
string | Yes | One or more post IDs, comma-separated (e.g., 123 or 123,456,789) |
Response:
Returns a JSON object with post IDs as keys and view counts as values:
{
"123": 1542,
"456": 893,
"789": 2341
}
Pro-Only Endpoints
site-views
Retrieves aggregate site-wide view statistics.
| Property | Value |
|---|---|
| Route | /wp-json/post-views-counter/site-views(?:/(?P<period>\d+))? |
| Methods | GET, POST |
| Callback | site_views_rest_api |
| Permission Callback | site_views_permissions_check |
| Source File | includes/class-rest_api.php |
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
period |
integer | No | Optional time period in days for filtering statistics |
default |
mixed | No | Default value if not specified |
validate_callback |
callable | No | Validation function for the parameter |
The period parameter is optional. When omitted, returns all-time statistics.
post-views
Retrieves view counts for one or more posts with extended data.
| Property | Value |
|---|---|
| Route | /wp-json/post-views-counter/post-views/(?P<id>(\d+,?)+) |
| Methods | GET, POST |
| Callback | post_views_rest_api |
| Permission Callback | post_views_permissions_check |
| Source File | includes/class-rest_api.php |
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
id |
string | Yes | One or more post IDs, comma-separated |
period |
string | No | Time period for filtering |
default |
mixed | No | Default value if not specified |
sanitize_callback |
callable | No | Sanitization function |
validate_callback |
callable | No | Validation function |
term-views
Retrieves view counts for taxonomy terms (categories, tags, custom taxonomies).
| Property | Value |
|---|---|
| Route | /wp-json/post-views-counter/term-views/(?P<id>(\d+,?)+) |
| Methods | GET, POST |
| Callback | term_views_rest_api |
| Permission Callback | term_views_permissions_check |
| Source File | includes/class-rest_api.php |
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
id |
string | Yes | One or more term IDs, comma-separated |
period |
string | No | Time period for filtering |
default |
mixed | No | Default value if not specified |
sanitize_callback |
callable | No | Sanitization function |
validate_callback |
callable | No | Validation function |
user-views
Retrieves view counts associated with specific users.
| Property | Value |
|---|---|
| Route | /wp-json/post-views-counter/user-views/(?P<id>(\d+,?)+) |
| Methods | GET, POST |
| Callback | user_views_rest_api |
| Permission Callback | user_views_permissions_check |
| Source File | includes/class-rest_api.php |
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
id |
string | Yes | One or more user IDs, comma-separated |
period |
string | No | Time period for filtering |
default |
mixed | No | Default value if not specified |
sanitize_callback |
callable | No | Sanitization function |
validate_callback |
callable | No | Validation function |
other-views
Retrieves view counts for custom object types configured in tracking settings.
| Property | Value |
|---|---|
| Route | /wp-json/post-views-counter/other-views/(?P<id>(\d+,?)+) |
| Methods | GET, POST |
| Callback | other_views_rest_api |
| Permission Callback | other_views_permissions_check |
| Source File | includes/class-rest_api.php |
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
id |
string | Yes | One or more custom object IDs, comma-separated |
period |
string | No | Time period for filtering |
default |
mixed | No | Default value if not specified |
sanitize_callback |
callable | No | Sanitization function |
validate_callback |
callable | No | Validation function |
view-term
Records a view event for a specific taxonomy term.
| Property | Value |
|———-|——-|
| Route | /wp-json/post-views-counter/view-term/(?P<id>\d+)|/view-term/ |
| Methods | POST |
| Callback | check_term_rest_api |
| Permission Callback | view_term_permissions_check |
| Source File | includes/class-rest_api.php |
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
id |
integer | Yes | The term ID to record a view for |
view-user
Records a view event for a specific user profile.
| Property | Value |
|———-|——-|
| Route | /wp-json/post-views-counter/view-user/(?P<id>\d+)|/view-user/ |
| Methods | POST |
| Callback | check_user_rest_api |
| Permission Callback | view_user_permissions_check |
| Source File | includes/class-rest_api.php |
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
id |
integer | Yes | The user ID to record a view for |
view-other
Records a view event for custom object types.
| Property | Value |
|———-|——-|
| Route | /wp-json/post-views-counter/view-other/(?P<id>\d+)|/view-other/ |
| Methods | POST |
| Callback | check_other_rest_api |
| Permission Callback | view_other_permissions_check |
| Source File | includes/class-rest_api.php |
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
id |
integer | Yes | The custom object ID to record a view for |
subtype |
string | No | Subtype identifier for the custom object |
storage_type |
string | No | Storage mechanism type |
storage_data |
mixed | No | Additional storage data |
default |
mixed | No | Default value if not specified |
sanitize_callback |
callable | No | Sanitization function |
Code Examples
Recording a Post View (JavaScript)
When using JavaScript Mode or REST API Mode, the plugin automatically handles view recording. For custom implementations:
fetch('/wp-json/post-views-counter/view-post/123', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': wpApiSettings.nonce // If authenticated
}
})
.then(response => response.json())
.then(data => console.log(data));
Retrieving Post Views (cURL)
curl -X GET "https://example.com/wp-json/post-views-counter/get-post-views/123"
Retrieving Multiple Post Views (cURL)
curl -X GET "https://example.com/wp-json/post-views-counter/get-post-views/123,456,789"
Retrieving Site-Wide Statistics (cURL)
curl -X GET "https://example.com/wp-json/post-views-counter/site-views/30"
This retrieves site-wide view statistics for the past 30 days.
Retrieving Term Views (JavaScript)
fetch('/wp-json/post-views-counter/term-views/15,23', {
method: 'GET'
})
.then(response => response.json())
.then(data => {
console.log('Category 15 views:', data['15']);
console.log('Category 23 views:', data['23']);
});
Retrieving User Views (cURL)
curl -X GET "https://example.com/wp-json/post-views-counter/user-views/1,2,3"
Retrieving Other Views (JavaScript)
fetch('/wp-json/post-views-counter/other-views/100,200', {
method: 'GET'
})
.then(response => response.json())
.then(data => {
console.log('Custom object 100 views:', data['100']);
console.log('Custom object 200 views:', data['200']);
});
