Developer Reference
PHP Helper Functions
Four global helper functions are available:
vc_get_post_views( $post_id ) — Returns the tracked view count (int). Defaults to the current post.
vc_set_post_views( $post_id, $count ) — Sets the view count to an exact value.
vc_increment_post_views( $post_id ) — Adds 1 and returns the new total.
vc_get_most_viewed_posts( $args ) — Returns a WP_Query ordered by views:
$popular = vc_get_most_viewed_posts( array(
'post_type' => 'post',
'posts_per_page' => 5,
) );
REST API Endpoints
Base namespace: /wp-json/view-counter/v1/
| Endpoint | Method | Auth | Description |
|---|---|---|---|
/track |
POST | Public | Record a view. Requires post_id |
/posts/{id} |
GET | Public | Get view count for a post |
/top |
GET | Public | Top posts. Params: post_type, limit |
/dashboard/month |
GET | Editor+ | Monthly stats. Param: month |
/posts/{id}/adjust |
POST | Editor+ | Set count. Requires count |
WP_Query Integration
Order any query by view count:
$query = new WP_Query( array(
'post_type' => 'post',
'orderby' => 'view_counter_views',
'order' => 'DESC',
) );
Or use the flag: 'vc_orderby_views' => true
Data Storage
View counts are stored in post meta under the key _vc_view_count.
Daily aggregate data is stored in a custom database table ({prefix}vc_daily_views) with columns: post_id, view_date, view_count.
You can query the meta key directly, but the helper functions are recommended for forward compatibility.