Last active
February 10, 2026 21:44
-
-
Save xnau/856b362ccdd7545cdfd02dbbb83b273e to your computer and use it in GitHub Desktop.
How to set up a prefilter for a Participants Database Participant Log list display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: PDB Log List Prefilter | |
| * Description: filters which log entries are presented in a log list | |
| * | |
| * @see https://gist.github.com/xnau/856b362ccdd7545cdfd02dbbb83b273e | |
| */ | |
| if ( !class_exists( 'xnau_filtered_log_template' ) ): | |
| class xnau_filtered_log_template | |
| { | |
| // name of the log that we will be filtering | |
| private $log_name = 'work_log'; | |
| // name of the log entry field we are filtering by | |
| private $filter_field = 'work_log_completion'; | |
| /* | |
| * this is the comparison operator we will use | |
| * | |
| * this can be one of: 'equals', 'not equals', 'greater than', 'less than' | |
| * | |
| * if you use greater than or less than, your comparison value must be numeric, | |
| * these operators won't work with string values! | |
| */ | |
| private $operator = 'equals'; | |
| /* | |
| * this is the comparison value we will use | |
| * | |
| * this can be a number or a string | |
| */ | |
| private $check_value = 0; | |
| // don't edit the stuff below here | |
| /** | |
| * @var bool true if the filter should be applied | |
| * | |
| * this is to make sure it's only applied where the special template is used | |
| */ | |
| private $set_filter = false; | |
| public function __construct() | |
| { | |
| add_filter( 'pdb-shortcode_call_pdb_record', [$this,'check_shortcode'] ); | |
| add_filter( 'pdb-shortcode_call_pdb_single', [$this,'check_shortcode'] ); | |
| // determine the operator | |
| switch ( $this->operator ) | |
| { | |
| case 'not equals': | |
| $this->operator = '<>'; | |
| break; | |
| case 'less than': | |
| $this->operator = '<'; | |
| break; | |
| case 'greater than': | |
| $this->operator = '>'; | |
| break; | |
| case 'equals': | |
| default: | |
| $this->operator = '='; | |
| } | |
| // this sets up the function that will alter the database query for the log | |
| add_filter( "pdblog-{$this->log_name}_record_entries_query", [ $this, 'alter_log_query' ] ); | |
| } | |
| /** | |
| * @param array $shortcode_attributes | |
| * @return array attributes | |
| */ | |
| public function check_shortcode( $shortcode_attributes ) | |
| { | |
| if ( isset( $shortcode_attributes['prefilter'] ) ) | |
| { | |
| $this->set_filter = true; | |
| } | |
| return $shortcode_attributes; | |
| } | |
| /* | |
| * adds the filtering clause to the query | |
| * | |
| * @global \wpdb $wpdb | |
| * @param string $query | |
| * @return string query | |
| */ | |
| public function alter_log_query( $query ) | |
| { | |
| if ( ! $this->set_filter ) | |
| { | |
| // if the filter is not enabled, return the unaltered query | |
| return $query; | |
| } | |
| global $wpdb; | |
| /* | |
| * this builds the filter clause we will add | |
| * the $wpdb->prepare() method makes sure the resulting query is valid and safe | |
| */ | |
| $filter_clause = $wpdb->prepare( 'WHERE %i ' . $this->operator . ' %s AND ', $this->filter_field, $this->check_value ); | |
| $altered_query = str_replace( 'WHERE', $filter_clause, $query ); | |
| return $altered_query; | |
| } | |
| } | |
| endif; | |
| new xnau_filtered_log_template; |
Author
Author
If you are a coder, there are all kinds of things you can do with this (like complex filtering on multiple field values) once you understand how it works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install this plugin, then edit it to your requirements.
How to Install a WordPress Plugin from a Gist
There are 4 properties at the top of the class that you will need to edit: The log name, filter field, comparison operator, and comparison value. The remainder of the class takes care of adding the filter to the database query.
Once you have edited the plugin, you should activate it.
The plugin will only alter the log list on shortcodes that enable it. Place the "prefilter" attribute into the shortcode to enable the prefilter in that shortcode display. For example:
[pdb_record prefilter=yes]This filter will work on the
[pdb_record]and[pdb_single]shortcodes only.