Back to Functions and API

Updated on Nov 05, 2023

Database API Methods

Slimstat defines a list of methods that can be used to retrieve information from the database. You can use these methods in your own plugin or theme to manipulate the data before it’s shown to the visitor. Here below you can find a list of available methods and their parameters.

public static function init( $_filters = '' )

Initializes the environment. Among the other things, it converts a user friendly list of filters (like for example browser contains Fire&&&platform contains win) into a code friendly array, available as wp_slimstat_db::$filters_normalized throughout the code. It takes one parameter, the string representing the filters you would like to apply to all the subsequent API calls. It can be called multiple times to reset or change the filters.

public static function count_records( $_column = 'id', $_where = '', $_use_date_filters = true )

Returns an integer representing the number of pageviews matching the filters set during the init. It takes three parameters:

  1. The dimension that should be counted (id, ip, resource, referrer, etc). Almost any column in the wp_slim_stats table can be used as a dimension;
  2. A custom SQL WHERE clause to append to the filters;
  3. Switch to not include date range filters in the SQL: by default Slimstat always limits all the results to the current month. This can be useful if you want to count, for example, how many pageviews have been recorded from the beginning. If your init call includes date filters (day, month, interval, etc), please make sure to set use_date_filters to true.
     
public static function get_recent( $_column = 'id', $_where = '', $_having = '', $_use_date_filters = true, $_as_column = '', $_more_columns = '' )

Returns an array of popular records matching the filters set during the init (order by pageview count). It take the following parameters:

  1. What dimension should be used. It completes the sentence “Retrieve the most recent ___ from the database” (IPs, resources, referrers, etc). Almost any column in the wp_slim_stats table can be used as a dimension;
  2. A custom SQL WHERE clause to append to the filters;
  3. An optional HAVING clause to filter results based on aggregate values (min, max, average, etc);
  4. Switch to not include date range filters in the SQL: by default Slimstat always limits all the results to the current month;
  5. An optional label to be appended to the column (dimension) name, in case your code requires results to be represented in a different way;
  6. Optional additional dimensions to be returned.