Template functions provide the primary PHP API for developers integrating Post Views Counter into themes and plugins. These functions handle database queries, caching, and output formatting internally, offering a clean interface for retrieving view counts, displaying most viewed content, and manually triggering view recording.
The template functions are defined in includes/functions.php and are organized into four categories:
- Retrieval functions: Fetch view counts from the database
- Display functions: Output formatted view counts directly
- Action functions: Manually trigger view recording or updates
- Utility functions: Convert periods and check conditions
API Reference
Retrieval Functions
pvc_get_post_views()
Retrieves the total view count for a specific post.
pvc_get_post_views( $post_id );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$post_id |
int | Post ID to retrieve views for. Defaults to current post in the loop. |
Returns: int – Total view count for the post.
Availability: Lite + Pro
REST Endpoint: /wp-json/post-views-counter/get-post-views/(?P<id>(\d+,?)+)
pvc_get_views()
Retrieves view counts with support for custom periods and filtering.
pvc_get_views( $args );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$args |
array | Query arguments for views retrieval. Specific parameters not specified in extracted inventory. |
Returns: int|array – View count or array of counts depending on arguments.
Availability: Lite + Pro
pvc_get_most_viewed_posts()
Retrieves an array of posts ordered by view count.
pvc_get_most_viewed_posts( $args );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$args |
array | Query arguments. Supports standard WP_Query parameters plus view-specific options. Specific parameters not specified in extracted inventory. |
Returns: array – Array of post objects ordered by views.
Availability: Lite + Pro
pvc_get_most_viewed_posts_args()
Retrieves the query arguments used by pvc_get_most_viewed_posts() for customization.
pvc_get_most_viewed_posts_args( $args );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$args |
array | Query arguments to modify. Specific parameters not specified in extracted inventory. |
Returns: array – Modified query arguments.
Availability: Pro-only
pvc_get_most_viewed_terms()
Retrieves taxonomy terms ordered by view count.
pvc_get_most_viewed_terms( $args );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$args |
array | Query arguments including taxonomy. Specific parameters not specified in extracted inventory. |
Returns: array – Array of term objects ordered by views.
Availability: Pro-only
REST Endpoint: /wp-json/post-views-counter/term-views/(?P<id>(\d+,?)+)
Related Settings: Enable at Post Views > Counting > Tracking Targets (taxonomies_count).
pvc_get_most_viewed_users()
Retrieves users ordered by their content’s view counts.
pvc_get_most_viewed_users( $args );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$args |
array | Query arguments. Specific parameters not specified in extracted inventory. |
Returns: array – Array of user objects with view data.
Availability: Pro-only
REST Endpoint: /wp-json/post-views-counter/user-views/(?P<id>(\d+,?)+)
Related Settings: Enable at Post Views > Counting > Tracking Targets (users_count).
pvc_get_site_views()
Retrieves total view counts across the entire site.
pvc_get_site_views( $period );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$period |
int|string | Time period for views. Accepts period ID or string. Default not specified in extracted inventory. |
Returns: int – Total site views for the specified period.
Availability: Pro-only
REST Endpoint: /wp-json/post-views-counter/site-views(?:/(?P<period>\d+))?
pvc_get_other_views()
Retrieves view counts for custom content types tracked outside standard posts.
pvc_get_other_views( $args );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$args |
array | Query arguments. Specific parameters not specified in extracted inventory. |
Returns: int|array – View count(s) for other content.
Availability: Pro-only
REST Endpoint: /wp-json/post-views-counter/other-views/(?P<id>(\d+,?)+)
Related Settings: Enable at Post Views > Counting > Tracking Targets (other_count).
pvc_get_other_views_period_where()
Generates SQL WHERE clause for period-based queries on other content views.
pvc_get_other_views_period_where( $period );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$period |
string | Period identifier. Specific format not specified in extracted inventory. |
Returns: string – SQL WHERE clause fragment.
Availability: Pro-only
pvc_get_other_views_query()
Builds a query for retrieving other content views.
pvc_get_other_views_query( $args );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$args |
array | Query arguments. Specific parameters not specified in extracted inventory. |
Returns: array – Query results.
Availability: Pro-only
pvc_get_post_views_period_where()
Generates SQL WHERE clause for period-based post views queries.
pvc_get_post_views_period_where( $period );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$period |
string | Period identifier. Specific format not specified in extracted inventory. |
Returns: string – SQL WHERE clause fragment.
Availability: Pro-only
pvc_get_most_searched_phrases()
Retrieves the most searched phrases from site search tracking.
pvc_get_most_searched_phrases( $args );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$args |
array | Query arguments. Specific parameters not specified in extracted inventory. |
Returns: array – Array of search phrases with count data.
Availability: Pro-only
Display Functions
pvc_post_views()
Outputs a formatted view count for the current or specified post.
pvc_post_views( $post_id );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$post_id |
int | Post ID. Defaults to current post in the loop. |
Returns: void – Echoes formatted output directly.
Availability: Lite + Pro
Note: Output respects settings from Post Views > Display > Counter Appearance including label, icon, and formatting options.
pvc_most_viewed_posts()
Outputs an HTML list of most viewed posts.
pvc_most_viewed_posts( $args );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$args |
array | Display arguments. Specific parameters not specified in extracted inventory. |
Returns: void – Echoes HTML list directly.
Availability: Lite + Pro
pvc_display_period_options()
Outputs HTML options for period selection dropdowns.
pvc_display_period_options( $selected, $echo );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$selected |
string | Currently selected period value. Default not specified in extracted inventory. |
$echo |
bool | Whether to echo or return. Default not specified in extracted inventory. |
Returns: string|void – HTML option elements if $echo is false.
Availability: Pro-only
Action Functions
pvc_view_post()
Manually triggers a view count for a post.
pvc_view_post( $post_id );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$post_id |
int | Post ID to record a view for. |
Returns: bool – True if view was recorded, false otherwise.
Availability: Lite + Pro
REST Endpoint: /wp-json/post-views-counter/view-post/(?P<id>\d+)
Note: Respects exclusion rules from Post Views > Counting > Visitor Exclusions.
pvc_update_post_views()
Updates view count directly in the database.
pvc_update_post_views( $post_id, $count );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$post_id |
int | Post ID to update. |
$count |
int | New view count value. |
Returns: bool|int – Number of rows affected or false on failure.
Availability: Lite + Pro
REST Endpoint: /wp-json/post-views-counter/update-post-views/
pvc_view_term()
Records a view for a taxonomy term.
pvc_view_term( $term_id );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$term_id |
int | Term ID to record a view for. |
Returns: bool – True if view was recorded.
Availability: Pro-only
REST Endpoint: /wp-json/post-views-counter/view-term/(?P<id>\d+)
pvc_view_user()
Records a view for a user profile.
pvc_view_user( $user_id );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$user_id |
int | User ID to record a view for. |
Returns: bool – True if view was recorded.
Availability: Pro-only
REST Endpoint: /wp-json/post-views-counter/view-user/(?P<id>\d+)
pvc_view_other()
Records a view for custom content.
pvc_view_other( $id );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$id |
int|string | Identifier for the custom content. |
Returns: bool – True if view was recorded.
Availability: Pro-only
REST Endpoint: /wp-json/post-views-counter/view-other/(?P<id>\d+)
Utility Functions
pvc_period2timestamp()
Converts a period string to Unix timestamp.
pvc_period2timestamp( $period );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$period |
string | Period string. Specific format not specified in extracted inventory. |
Returns: int – Unix timestamp.
Availability: Lite + Pro
pvc_period2date()
Converts a period string to a formatted date.
pvc_period2date( $period, $format );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$period |
string | Period string. Specific format not specified in extracted inventory. |
$format |
string | PHP date format. Default not specified in extracted inventory. |
Returns: string – Formatted date string.
Availability: Lite + Pro
pvc_post_views_has_content_column()
Checks if the post views column is enabled for a post type.
pvc_post_views_has_content_column( $post_type );
Parameters:
| Parameter | Type | Description |
|---|---|---|
$post_type |
string | Post type to check. |
Returns: bool – True if column is enabled for the post type.
Availability: Lite + Pro
Related Settings: Post Views > Display > Admin Interface (post_views_column)
Code Examples
Basic View Count Display
Display the view count for the current post:
// Inside the loop - raw count
echo pvc_get_post_views() . ' views';
// Or use the display function for formatted output
pvc_post_views();
Get Views for a Specific Post
$post_id = 42;
$views = pvc_get_post_views( $post_id );
printf( 'This post has %d views', $views );
Most Viewed Posts Query
Retrieve the top 5 most viewed posts:
$popular = pvc_get_most_viewed_posts( array(
'posts_per_page' => 5,
'post_type' => 'post',
'post_status' => 'publish',
) );
if ( $popular ) {
echo '<ul class="top-posts">';
foreach ( $popular as $post ) {
printf(
'<li><a href="%s">%s</a> - %d views</li>',
get_permalink( $post->ID ),
get_the_title( $post->ID ),
pvc_get_post_views( $post->ID )
);
}
echo '</ul>';
}
Display Most Viewed Posts List
Use the display function to output a formatted list:
// Output an HTML list
pvc_most_viewed_posts( array(
'number_of_posts' => 5,
'post_type' => 'post',
) );
Manual View Recording
Programmatically record a view for a specific post:
// Record a view for post ID 123
$recorded = pvc_view_post( 123 );
if ( $recorded ) {
error_log( 'View recorded successfully' );
}
Update View Count Directly
Set a specific view count (useful for migrations or corrections):
// Set post 42 to exactly 1000 views
$result = pvc_update_post_views( 42, 1000 );
if ( is_int( $result ) && $result > 0 ) {
error_log( "Updated {$result} row(s)" );
}
Check Post Type Column Support
Conditionally display views based on column settings:
if ( pvc_post_views_has_content_column( 'product' ) ) {
echo '<span class="product-views">';
pvc_post_views( $product_id );
echo '</span>';
}
Site-Wide Views (Pro)
Get total views across the entire site:
// Total all-time site views
$total_views = pvc_get_site_views
