Skip to content

Instantly share code, notes, and snippets.

@ka215
Created June 15, 2016 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ka215/bc882a551bf88295071022ff8e233b67 to your computer and use it in GitHub Desktop.
Save ka215/bc882a551bf88295071022ff8e233b67 to your computer and use it in GitHub Desktop.
If you want to filter in each years at the datetime type column as like the "date" type, you will resolve by using this filter hooks. However, because some plugin core sources should be modified, it's not able to work correctly in the current version (2.1.32). In the next version 2.1.33, I'm going to support.
<?php
// Note: this code will be enabled since the CDBT plugin version 2.1.33.
function add_filtered_column_sql( $sql, $table_name, $sql_clauses ) {
$target_tables = [ 'your_table_name' ];
$filter_date_column = 'date_type_column'; // Column name that you want to filter by the filtering dropdown box
if ( in_array( $table_name, $target_tables ) && strpos( $sql_clauses[0], $filter_date_column ) !== false ) {
$select_clause = $sql_clauses[0] .',CAST(YEAR(`'. $filter_date_column .'`) AS CHAR) AS `year`'; // "year" is alias column for filtering
$_overwrite_sql = <<< SQL
SELECT %s
FROM %s
%s
%s %s
SQL;
$sql = sprintf( $_overwrite_sql, $select_clause, $table_name, $sql_clauses[1], $sql_clauses[2], $sql_clauses[3] );
}
return $sql;
}
add_filter( 'cdbt_crud_get_data_sql', 'add_filtered_column_sql', 10, 3 );
function change_custom_filter_option( $component_options, $shortcode_name, $table ){
$target_tables = [ 'your_table_name' ];
$target_shortcodes = [ 'cdbt-view' ];
$filter_alias_column = 'year'; // Filtering alias column defined in the above filter hook
if ( in_array( $table, $target_tables ) && in_array( $shortcode_name, $target_shortcodes ) ) {
$component_options['enableFilter'] = true;
$component_options['filter_column'] = $filter_alias_column;
$start_year = 2001; // Start year of the displayed in the pull down lists
$end_year = intval( date( 'Y' ) );
for ( $y = $start_year; $y <= $end_year; $y++ ) {
$component_options['filters'][] = strval( $y );
}
}
return $component_options;
}
add_filter( 'cdbt_shortcode_custom_component_options', 'change_custom_filter_option', 10, 3 );
@ka215
Copy link
Author

ka215 commented Jun 15, 2016

If you want to work at current plugin version 2.1.32, you should modify of "templates/components/cdbt_table.php" in the plugin directory as following.

Insert below to line 68:

$_list_value = $_list_value === '0' ? $_value[0] : $_list_value;

Modify below to line 808 (809):

      if (item[searchObj.column] === String(searchObj.keyword)) {

Thank you,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment