Search results for ""

Sorry, no results found. Perhaps you would like to search the community forum?
All Topics

Getting range filtering to work for custom field (type: date)

I was having trouble getting the range filtering to work for custom date fields.

For some reason the “Filter by:” option was not showing up when the Field Type was set to “Date”. When the field type is set to “Numeric”, you can see the “Filter by:” option.

After digging through the code, I found out why, the “filterable_type” property was not being set to “date” for the date field types, whereas for “numeric” field type the “filterable_type” property was set to “numeric”. The can be seen in /admin-columns-pro/classes/filtering/classes/model.php (Line 87: enable_filtering_custom_field).
To fix this, add the following code:

if ( in_array( $column->options->field_type, array( 'date' ) ) ) {
$column->set_properties( 'filterable_type', 'date' );


if ( in_array( $column->options->field_type, array( 'numeric' ) ) ) {
$column->set_properties( 'filterable_type', 'numeric' );

Now, you should be able to select a “Filter by:” option for custom date fields.

After adding a range filterable date field, none of my other filters were showing up. It turns out that in the “add_filtering_markup” function, every column was considered to have “filter_format” set to range. Which was strange because when i cross-checked with the configurations in the database, only the custom date field was filterable with filter_format set to “range”. After hours of debugging, it turns out ACP clones the first column-meta (if there are more than one custom field columns), when cloning, it copies the filter_format => range into the other columns.
To fix this, add the following code right before the array is returned:
/admin-columns-pro/codepress-admin-columns/classes/column/custom-field.php (Line 79: sanitize_options)

if (isset($options['filter']) && ! isset($options['filter_format'])) {
$options['filter_format'] = '';

Last but not least, the date range filters weren’t working properly. Turns out ACP was formatting the requested min & max dates to Ymd and comparing it numerically. The problem was, the date was saved as “Y-m-d H:i:s” format. In order to solve this I modified the following file:
/admin-columns-pro/classes/filtering/classes/model.php (Line 201: get_meta_query_range)
Replace the date query logic with:

if ( $min ) {
$min = DateTime::createFromFormat('Y-m-d', $min);
$meta_query[] = array(
'key' => $key,
'value' => $min->format('Y-m-d'),
'compare' => '>=',
'type' => 'DATE'
if ( $max ) {
$max = DateTime::createFromFormat('Y-m-d', $max);
$meta_query[] = array(
'key' => $key,
'value' => $max->format('Y-m-d'),
'compare' => '<=',
'type' => 'DATE'

I hope this helps someone, it’s a shame this didn’t work out of the box (now i have to make sure I update the code after ACP updates….).

For something I paid $200 for, i expect these issues to be non-existent.

3 years, 7 months ago


Thank you for you detailed description on how you enabled range filtering for Custom Type dates.
There is a reason why range filtering for Custom Field dates is not available, yet.

Before I will explain why some of your fixes are not applied in our code, I like to say that we definitely will take a look on how we can support date range filtering for Custom Fields.

We started to implement filtering on date ranges from our 3.7 release just for ACF fields. The reason for this is because ACF always stores dates in the same format, as Ymd. That is the reason why you found that we were filtering on numeric values and why it did not work for you meta fields with format ‘Y-m-d’. In your case it’s ‘Y-m-d’ but for Custom Fields we never know which format is used as it can be anything and that is the reason why we did not yet enabled this for Custom fields.

Please be aware that your changes will not work anymore for ACF date fields for which it originally is intended. Also if you ever have another format for dates in your custom fields it will also break the filtering. Therefore we do not advice to make these changes in the core. My colleague will look at this and see if we can make add filters for you in our code so you can enable date filtering for custom fields in your own plugin/code without affecting the core.

You’re mention about sanitizing the options when creating/cloning a new field is valid and we will fix this in our product. Thanks for noticing!

3 years, 7 months ago

Hi Vivek,

Although we do not actively support Custom Field date ranges, we do not want to prevent you from doing so.

This afternoon we released a hotfix, version 3.7.2, which also includes the meta query you suggested. I had to replace the ‘DateTime::createFromFormat’ because it’s only available since PHP 5.3 (WordPress still supports 5.2.4).

Instead of modifying the core plugin, you can now enable range filtering for Custom Fields with a filter, allowing you to savely update the plugin in the future.

Add this to your theme’s functions.php (or plugin):

function ac_set_meta_column_type_to_date_format( $columns ) {
	foreach ( $columns as $column ) {
		if ( 'column-meta' == $column->get_type() && 'date' == $column->get_field_type() ) {
			$column->set_properties( 'filterable_type', 'date_format' );

add_action( "cac/columns", 'ac_set_meta_column_type_to_date_format' );

Cheers, Tobias

3 years, 7 months ago

Thanks guys, i’ve got this working with the action now!

3 years, 5 months ago
Jon Klassen

Is this fix still relevant?

I am having the same issue trying to get custom dates fields filtered by date ranges

1 month ago

You must be logged in to reply to this topic.