Guide to Using the /top REST API Endpoint in Init View Count

The Init View Count plugin provides the /wp-json/initvico/v1/top REST API endpoint to fetch the most viewed posts. This is a powerful tool for frontends, SPAs, mobile apps, or headless WordPress setups. This article provides a detailed guide on how to use the endpoint, supported parameters, and how to extend it using filters.

Guide to Using the /top REST API Endpoint in Init View Count

API Overview

1. View Counter Endpoint

POST /wp-json/initvico/v1/count

Increases the view count of a post. Typically called from frontend JavaScript.

Required parameters

  • post_id: ID of the post to be counted

Response

{
  "post_id": 123,
  "total": 103,
  "total_formatted": "103",
  "total_short": "103",
  "day": 5,
  "week": 27,
  "month": 58
}

The day, week, and month fields appear only if enabled in plugin settings.

2. Top Posts Endpoint

GET /wp-json/initvico/v1/top

Purpose

Returns a list of the most viewed posts based on time range, taxonomy, post type, and more.

Supported Parameters

  • range: Time range to query. Options: total (default), day, week, month
  • page: Paginate results. Combine with number for additional pages (e.g. page=2)
  • number: Number of posts to return (default: 5)
  • post_type: Post type to query (default: post)
  • fields: Return format: minimal (id, title, link) or full (default)
  • tax: Any taxonomy name (e.g. category, genre)
  • terms: List of term slugs or IDs, comma-separated. The plugin auto-detects based on the first entry (numeric or not).
  • no_cache: Set to 1 to force query and bypass cache

Example Usage

Fetch 6 popular posts this week

GET /wp-json/initvico/v1/top?range=week&number=6

Fetch truyen post type within taxonomy genre=action,comedy

GET /wp-json/initvico/v1/top?post_type=truyen&tax=genre&terms=action,comedy

Return simplified data (id, title, link only)

GET /wp-json/initvico/v1/top?fields=minimal

Bypass cache (always query fresh)

GET /wp-json/initvico/v1/top?no_cache=1

Response Format

With fields=full (default), the response includes:

[
  {
    "id": 123,
    "title": "Post Title",
    "link": "https://example.com/post-title",
    "excerpt": "...",
    "views": 1283,
    "thumbnail": "https://example.com/wp-content/uploads/2025/05/thumbnail.jpg",
    "post_type": "post",
    "type": "Post",
    "category": "News",
    "date": "2025-05-18"
  }
]

If no featured image is set, a default thumbnail from the plugin will be used.

With fields=minimal, the response is simpler:

[
  {
    "id": 123,
    "title": "Post Title",
    "link": "https://example.com/post-title"
  }
]

Extending the API with Filters

1. init_plugin_suite_view_count_api_top_args

Customize the WP_Query arguments:

add_filter('init_plugin_suite_view_count_api_top_args', function ($args, $request) {
    $args['post__not_in'] = [42]; // Exclude post ID 42
    return $args;
}, 10, 2);

2. init_plugin_suite_view_count_api_top_item

Customize the response item for each post:

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

3. init_plugin_suite_view_count_api_top_cache_time

Adjust the cache duration (default: 5 minutes):

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

Notes

  • Uses WP_Query to ensure accurate sorting and full compatibility
  • Supports any taxonomy — not limited to category or tag
  • Ideal for AJAX, JavaScript frontends, SPAs, or headless integrations

Comments


  • No comments yet.

Web-Based Tools

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

Login