• Resolved darylldelfin

    (@darylldelfin)


    Hi P?r,

    Really awesome plugin.

    I need to know how I am able to query simplehistory logs so that I can return logs with this criteria :

    logger : Audit_UserManagementLogger
    user_id : 469
    old_value : Daryll
    new_value : Daryll Updated

    I am using this query but it only retrieves all queries, not based on custom fields.

    $query = new SimpleHistoryLogQuery();
    
    $query_args = array(
    	"paged" => 1,
    	"posts_per_page" => 20
    );
    
    $events = $query->query($query_args);
    

    How can I do this?
    Thanks,
    Daryll

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author P?r Thernstr?m

    (@eskapism)

    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"
    );
    
    Thread Starter darylldelfin

    (@darylldelfin)

    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?

    Plugin Author P?r Thernstr?m

    (@eskapism)

    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.

    Thread Starter darylldelfin

    (@darylldelfin)

    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

    Thread Starter darylldelfin

    (@darylldelfin)

    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;
        	} );
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom SimpleHistoryLogQuery() query with context field = ”’ is closed to new replies.