Query API

You can get things for the history by using the class SimpleHistoryLogQuery.

A quick example that displays the latest 5 rows from the log:


function simple_history_test_output() {
    $log_query = new \Simple_History\Log_Query;
    $simple_history = \Simple_History\Simple_History::get_instance();

    $query_results = $log_query->query( [
        'posts_per_page' => 5,
    ] );

    printf(
        '
            <p>Found %1$d rows.</p>
            <p>Viewing page %2$d of %3$d.</p>
        ',
        esc_html( $query_results['total_row_count'] ), // 1
        esc_html( $query_results['page_current'] ), // 2
        esc_html( $query_results['pages_count'] ), // 3
    );

    echo "<ul>";
    
    foreach ( $query_results['log_rows'] as $row ) {
        $header_output = $simple_history->getLogRowHeaderOutput( $row );
        $text_output = $simple_history->getLogRowPlainTextOutput( $row );
        $details_output = $simple_history->getLogRowDetailsOutput( $row );

        echo "<li>";
        echo "<hr />";
        echo "<p>{$header_output}</p>";
        echo "<p>{$text_output}</p>";
        echo "<p>{$details_output}</p>";
        echo "</li>";
    }
    
    echo "</ul>";
}

Code language: PHP (php)

When running the above function the output will look something like this:

simple-history-log-query-example

Available Parameters

The query() method accepts an array of parameters to filter and paginate results:

Basic Parameters

ParameterTypeDescription
posts_per_pageintNumber of events to return per page. Default: 10
pagedintPage number for pagination. Default: 1
date_fromstringFilter events from this date (YYYY-MM-DD format)
date_tostringFilter events until this date (YYYY-MM-DD format)
searchstringSearch for events containing this text

Inclusion Filters

Use these parameters to include only events matching specific criteria:

ParameterTypeDescription
loggersarrayInclude only events from these loggers (e.g., ['SimplePostLogger', 'SimpleUserLogger'])
loglevelsarrayInclude only events with these log levels (e.g., ['warning', 'error'])
messagesarrayInclude only specific message types (format: ['LoggerSlug:MessageKey'])
userintInclude only events by this user ID
usersarrayInclude only events by these user IDs
initiatorstring|arrayInclude only events by these initiators

Exclusion Filters (Negative Filters)

Use these parameters to exclude events matching specific criteria. This is useful when you want to see all events except certain types:

ParameterTypeDescription
exclude_loggersarrayExclude events from these loggers
exclude_loglevelsarrayExclude events with these log levels
exclude_messagesarrayExclude specific message types (format: ['LoggerSlug:MessageKey'])
exclude_userintExclude events by this user ID
exclude_usersarrayExclude events by these user IDs
exclude_initiatorstring|arrayExclude events by these initiators
exclude_searchstringExclude events containing this text

Note: When the same value appears in both an inclusion and exclusion filter, exclusion takes precedence.

Valid Initiator Values

  • wp_user – Regular WordPress user actions
  • wp_cli – Actions performed via WP-CLI
  • wp – WordPress system actions (cron jobs, automatic updates)
  • web_user – Non-logged-in web visitors
  • other – Other sources

Examples Using Exclusion Filters

Here are some practical examples using exclusion filters:


$log_query = new \Simple_History\Log_Query();

// Exclude debug level events
$results = $log_query->query([
    'posts_per_page' => 50,
    'exclude_loglevels' => ['debug'],
]);

// Exclude WordPress system events (cron, auto-updates)
$results = $log_query->query([
    'posts_per_page' => 50,
    'exclude_initiator' => ['wp', 'wp_cron'],
]);

// Exclude events containing "cron" in the text
$results = $log_query->query([
    'posts_per_page' => 50,
    'exclude_search' => 'cron',
]);

// Exclude specific message types
$results = $log_query->query([
    'posts_per_page' => 100,
    'exclude_messages' => [
        'SimplePluginLogger:plugin_activated',
        'SimplePluginLogger:plugin_deactivated',
    ],
]);

// Combine inclusion and exclusion: 
// Show user login events, but exclude admin user
$results = $log_query->query([
    'posts_per_page' => 50,
    'loggers' => ['SimpleUserLogger'],
    'exclude_user' => 1, // Exclude admin (user ID 1)
]);

Code language: PHP (php)