To only get log entries from the logger “Audit_UserManagementLogger” you should be able to use query args like this:
$query_args = array(
"paged" => 1,
"posts_per_page" => 20,
"loggers" => "Audit_UserManagementLogger"
);
Hi Pär, Thanks. I’d like to search for not just the logger, I’d like to search for a value from a custom context field. Is this possible?
Hm, not sure if that’s possible at the moment. It’s a good idea however so I will add it to my list of feature requests!
There is however some filters you can use to modify the SQL query used by Simple History to search/get the log. For example simple_history/log_query_sql_where.
Take a look in the file SimpleHistoryLogQuery.php for more filters and with some trial and error you may solve it that way.
I see, I also checked SimpleHistoryLogQuery.php and you’re right, I can’t search via custom context field straight out of the box. I’m looking forward for this feature request!
Thanks,
Daryll
To anyone who want to do this, this was how I was able to do it :
I wanted to retrieve log records where a context key named ‘user_id’ has a value of the selected user.
add_filter( 'simple_history/log_query_inner_where', function($where) {
global $wpdb;
if (isset($_GET['editUser'])) {
$user_id = $_GET['editUser'];
}else{
$user_id = get_current_user_id();
}
$where .= sprintf('
AND id IN (
SELECT id
# , c1.history_id, c2.history_id
FROM %1$s AS h
INNER JOIN %2$s AS c2
ON c2.history_id = h.id
AND c2.key = "user_id"
AND c2.value = "%3$s"
)
', $wpdb->prefix.'simple_history', $wpdb->prefix.'simple_history_contexts', $user_id);
return $where;
} );