Post Views Counter provides shortcodes for displaying view counts within post content, widget areas, and template files. The primary shortcode [post-views] is available in both Lite and Pro versions, while [site-views] provides site-wide statistics in the Pro version.

Shortcodes integrate with display settings configured at Post Views > Display > Counter Appearance and respect visibility rules defined at Post Views > Display > Display Audience. When the position setting at Post Views > Display > Display Targets is set to Manual only, shortcodes become the primary method for displaying view counts.

API Reference

[post-views]

Displays the view count for a specific post. Available in both Lite and Pro versions.

File: includes/class-frontend.php

Syntax:

[post-views]
[post-views id="123"]

Attributes

Attribute Type Default Description
id integer Current post ID The post ID to display views for. Defaults to the current post in the loop.

The id attribute allows displaying views for any post from any location. When omitted, the shortcode uses get_the_ID() to retrieve the current post’s ID within the WordPress loop.

Output Format

The shortcode output respects settings from Post Views > Display > Counter Appearance:

  • Label (display.label): Text displayed before the count. Default: Post Views:
  • Display Style (display.display_style): Controls whether to show icon, label text, or both. Values: icon (Icon), text (Label)
  • Icon Class (display.icon_class): WordPress dashicon class applied to the icon element. Default: dashicons-chart-bar
  • Number Formatting (display.use_format): Formats numbers with thousands separators according to site locale. Default: True

The label and class (icon class) are controlled through plugin settings rather than shortcode attributes. This ensures consistent styling across all shortcode instances. To customize these values:

  1. Navigate to Post Views > Display > Counter Appearance
  2. Modify the Views Label field for custom label text
  3. Update the Icon Class field with any Dashicons class (e.g., dashicons-chart-bar, dashicons-visibility)

Return Value

Returns HTML markup containing the formatted view count. When display.position is set to Manual only, shortcodes become the primary method for displaying counts.

Filter Hook

The output can be customized using the add_post_views_count filter:

apply_filters( 'add_post_views_count', $html, $post_id );

Parameters:

  • $html (string) — The generated HTML markup
  • $post_id (integer) — The post ID

[site-views]

Displays site-wide view statistics. Pro-only shortcode.

File: includes/class-frontend.php

Syntax:

[site-views]
[site-views period="7"]

Attributes

Attribute Type Default Description
period integer Total views Number of days to calculate views for. Omit for total views.

Return Value

Returns HTML markup containing the site-wide view count for the specified period.

REST API Endpoint: /wp-json/post-views-counter/site-views(?:/(?P<period>\d+))?

Code Examples

Basic Usage

Display views for the current post using the default id (current post):

// Within a template file
echo do_shortcode( '[post-views]' );

Specific Post ID

Display views for a specific post by ID using the id attribute:

// Display views for post ID 123
echo do_shortcode( '[post-views id="123"]' );

Customizing Label and Class

The label and icon class are controlled through Post Views > Display > Counter Appearance. To customize these settings:

  1. Navigate to Post Views > Display > Counter Appearance
  2. Modify the Views Label (label) field for custom text
  3. Update the Icon Class (icon_class) field with any Dashicons class

To programmatically modify output:

// Filter to modify the output
add_filter( 'add_post_views_count', function( $html, $post_id ) {
    // The label comes from display.label setting
    // The icon class comes from display.icon_class setting
    return $html;
}, 10, 2 );

Conditional Display

Show views only for certain post types:

// In a template file
if ( is_singular( 'post' ) ) {
    echo do_shortcode( '[post-views]' );
}

Widget Area Usage

The shortcode works in widget areas when added to a Text widget or HTML widget:

This post has been viewed [post-views] times.

Integration with Template Functions

Combine shortcode with template functions for advanced usage:

// Get raw view count
$views = pvc_get_post_views( get_the_ID() );

// Conditionally display shortcode
if ( $views > 100 ) {
    echo '<div class="popular-badge">';
    echo do_shortcode( '[post-views]' );
    echo '</div>';
}

Site-Wide Statistics

Display total site views:

echo do_shortcode( '[site-views]' );

Display views from the last 30 days:

echo do_shortcode( '[site-views period="30"]' );

REST API Integration

Fetch site views via REST API:

// JavaScript example
fetch( '/wp-json/post-views-counter/site-views/7' )
    .then( response => response.json() )
    .then( data => console.log( 'Weekly views:', data ) );

Customizing Output with Filters

Modify shortcode output using the add_post_views_count filter:

// Customize the view count display
add_filter( 'add_post_views_count', function( $html, $post_id ) {
    $views = pvc_get_post_views( $post_id );
    
    if ( $views > 1000 ) {
        return '<span class="trending">' . $html . '</span>';
    }
    
    return $html;
}, 10, 2 );

Term and User Views Filters

Pro version provides additional filters for term and user views:

// Customize term views display
add_filter( 'add_term_views_count', function( $html, $term_id ) {
    return '<span class="term-views">' . $html . '</span>';
}, 10, 2 );

// Customize user views display
add_filter( 'add_user_views_count', function( $html, $user_id ) {
    return '<span class="user-views">' . $html . '</span>';
}, 10, 2 );

Manual Display Position

When Post Views > Display > Display Targets > position is set to Manual only, use shortcodes for precise placement:

// In single.php template
<article>
    <header>
        <h1><?php the_title(); ?></h1>
        <div class="meta">
            <?php echo do_shortcode( '[post-views]' ); ?>
        </div>
    </header>
    
    <?php the_content(); ?>
</article>