Back to Filters

Updated on Jun 10, 2024

slimstat_get_results_sql

This filter allows you to customize the SQL query that fetches data from the database for a specific WP Slimstat report. You can use it to add, remove, or modify parts of the query, such as the SELECT, WHERE, GROUP BY, or ORDER BY clauses.

Usage

$_sql = apply_filters('slimstat_get_results_sql', $_sql, $_select_no_aggregate_values, $_order_by, $_group_by, $_aggregate_values_add);

Explanation

  • $_sql: The original SQL query that WP Slimstat uses to fetch data for a report.
  • $_select_no_aggregate_values: An array of column names that are being selected without any aggregation (like ip, browser, etc.).
  • $_order_by: The name of the column used to sort the results (like dt for sorting by date).
  • $_group_by: The name of the column used to group the results (like country to group by countries).
  • $_aggregate_values_add: An array of columns with aggregation functions, like COUNT(*) or SUM(length_in_seconds).

Example

add_filter('slimstat_get_results_sql', 'my_custom_slimstat_query', 10, 5);

function my_custom_slimstat_query($sql, $select, $order_by, $group_by, $aggregate) {
    // Example: Add a WHERE clause to filter out certain IPs
    $sql .= " AND ip NOT IN ('127.0.0.1', '::1')";
    
    return $sql;
}

This filter is powerful but use it carefully, as incorrect modifications can break your reports or impact performance.