Created
June 15, 2016 06:33
-
-
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.
This file contains 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 | |
// 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Modify below to line 808 (809):
Thank you,