Extending Init View Count with Filters & Hooks – A Developer’s Guide

The Init View Count plugin is built with extensibility in mind. As a developer, you can easily customize its behavior through available filters and actions. This guide lists all supported hooks and how to use them effectively.

Extending Init View Count with Filters & Hooks – A Developer’s Guide

New Filters (v1.15)

1. init_plugin_suite_view_count_engagement_meta_keys

Change the meta keys used to retrieve like and share counts when calculating engagement quality.

Applies to: Engagement algorithm

Params: array $meta_keys (likes, shares), int $post_id

add_filter('init_plugin_suite_view_count_engagement_meta_keys', function ($meta_keys, $post_id) {
    // Example: use custom meta keys for likes and shares
    return [
        'likes'  => '_my_like_count',
        'shares' => '_my_share_count',
    ];
}, 10, 2);

2. init_plugin_suite_view_count_trending_post_types

Override the list of post types used by the Trending cron calculation.

Applies to: Cron Trending

Params: array $post_types

add_filter('init_plugin_suite_view_count_trending_post_types', function ($post_types) {
    // Only calculate Trending for the "manga" post type
    return ['manga'];
}, 10, 1);

3. init_plugin_suite_view_count_trending_component_weights

Adjust weights for Trending score components.

Applies to: Trending algorithm

Params: array $weights (velocity, engagement, freshness, momentum)

add_filter('init_plugin_suite_view_count_trending_component_weights', function ($weights) {
    // Increase velocity weight, reduce freshness weight
    $weights['velocity']  = 0.45;
    $weights['engagement'] = 0.30;
    $weights['freshness'] = 0.10;
    $weights['momentum']  = 0.15;
    return $weights;
}, 10, 1);

Filters Related to the REST API

The following filters allow you to customize how view counting works, modify API results, and control shortcode output.

1. init_plugin_suite_view_count_should_count

Determines whether the current view should be counted (e.g. skip logged-in users, bots, etc.).

add_filter('init_plugin_suite_view_count_should_count', function ($should_count, $post_id, $request) {
    // Skip counting if the user is logged in
    if (is_user_logged_in()) return false;
    return $should_count;
}, 10, 3);

2. init_plugin_suite_view_count_meta_key

Override the default meta key _init_view_count used for total views. Applies to both REST API and shortcode.

add_filter('init_plugin_suite_view_count_meta_key', function ($key, $post_id = null) {
    // Replace only the total views key
    if ($key === '_init_view_count') {
        return '_true_read_count';
    }
    return $key; // Leave day/week/month keys unchanged
}, 10, 2);

3. init_plugin_suite_view_count_after_counted

Runs after a view has been counted. Useful for logging, analytics, notifications, etc.

add_action('init_plugin_suite_view_count_after_counted', function ($post_id, $updated, $request) {
    // Log the new total after counting
    error_log("Post {$post_id} now has {$updated['total']} views.");
}, 10, 3);

4. init_plugin_suite_view_count_api_top_args

Modify the WP_Query arguments used in the /wp-json/initvico/v1/top API.

add_filter('init_plugin_suite_view_count_api_top_args', function ($args, $request) {
    // Only return posts where _is_featured = 1
    $args['meta_query'][] = [
        'key'     => '_is_featured',
        'value'   => '1',
        'compare' => '='
    ];
    return $args;
}, 10, 2);

5. init_plugin_suite_view_count_api_top_item

Customize each item in the /top API response.

add_filter('init_plugin_suite_view_count_api_top_item', function ($item, $post, $request) {
    // Add author name to each result item
    $item['author'] = get_the_author_meta('display_name', $post->post_author);
    return $item;
}, 10, 3);

6. init_plugin_suite_view_count_api_top_cache_time

Change the cache duration (in seconds) for /top results. Default is 300 seconds (5 minutes).

add_filter('init_plugin_suite_view_count_api_top_cache_time', function ($ttl, $request) {
    return 600; // Cache results for 10 minutes
}, 10, 2);

7. init_plugin_suite_view_count_top_post_types

Customize the list of post_type used in the /top API. Defaults to ['post', 'page'].

add_filter('init_plugin_suite_view_count_top_post_types', function ($post_types, $request) {
    // Only include posts of type "manga"
    return ['manga'];
}, 10, 2);

Shortcode Filters

1. init_plugin_suite_view_count_query_args

Customize the WP_Query arguments for the [init_view_list] shortcode.

add_filter('init_plugin_suite_view_count_query_args', function ($args, $atts) {
    // Exclude a specific post ID
    $args['post__not_in'] = [42];
    return $args;
}, 10, 2);

2. init_plugin_suite_view_count_empty_output

Customize the HTML output shown when there are no results.

add_filter('init_plugin_suite_view_count_empty_output', function ($output, $atts) {
    // Return a custom message if no posts match
    return '<p class="no-posts">No popular posts found.</p>';
}, 10, 2);

3. init_plugin_suite_view_count_view_list_atts

Modify the shortcode attributes after they’ve been merged with defaults.

add_filter('init_plugin_suite_view_count_view_list_atts', function ($atts) {
    // Show more posts on the homepage
    if (is_page('home')) {
        $atts['number'] = 10;
    }
    return $atts;
});

4. init_plugin_suite_view_count_default_shortcode

Customize the content of the shortcode that is automatically inserted into post content (when auto-insert is enabled).

add_filter('init_plugin_suite_view_count_default_shortcode', function ($shortcode) {
    // Use a simpler shortcode that shows raw view count only
    return '[init_view_count format="raw"]';
});

5. init_plugin_suite_view_count_auto_insert_enabled

Control whether auto-insert is enabled based on post type or position (before/after content).

add_filter('init_plugin_suite_view_count_auto_insert_enabled', function ($enabled, $position, $post_type) {
    // Disable auto insert for post type "page"
    if ($post_type === 'page') {
        return false;
    }
    return $enabled;
}, 10, 3);

Developer Notes

  • Register filters in a custom plugin or your theme’s functions.php file.
  • Always validate input variables like $post_id and $request to prevent bugs or security issues.
  • Be cautious when modifying meta_key or query args as they can affect view logic and displayed results.

Comments


  • No comments yet.

Init Toolbox

Press Ctrl + \ on desktop, or swipe left anywhere on mobile.

Login