Docs / Developer / Developer Reference

Developer Reference

REST API endpoints, PHP helper functions, hooks, and query integration.

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 endpoint; accepts optional nonce 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 Logged-in users with edit_posts Monthly dashboard stats. Optional param: month in YYYY-MM format
/posts/{id}/adjust POST Users who can edit that post Set count. Requires count
/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

All-time view counts are stored in post meta under the key _vc_view_count.

Daily site-wide totals are stored in {prefix}vc_daily_views:

day_date date
view_count bigint unsigned

Per-post daily history is stored in {prefix}vc_daily_post_views:

day_date date
post_id bigint unsigned
view_count bigint unsigned

You can query the meta key directly, but the helper functions are recommended for forward compatibility.

Hooks

vc_format_count — filter the formatted count string.

add_filter( 'vc_format_count', function( $formatted, $count ) {
    return $count >= 1000 ? round( $count / 1000, 1 ) . 'k' : $formatted;
}, 10, 2 );

vc_can_count_view — filter whether a view should be counted.

add_filter( 'vc_can_count_view', function( $allowed, $post_id, $source, $post ) {
    return $allowed;
}, 10, 4 );

vc_post_view_counted — action fired after a view is counted.

add_action( 'vc_post_view_counted', function( $post_id, $count, $source ) {
    // Run custom logic after a view is counted.
}, 10, 3 );