Support

Search results for ""

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

Support custom ACF date field values for filtering

Hey guys,

I noticed that if using a custom stored value for ACF datepicker fields, filtering is not possible by this field.
Is there a way to change this behavior with some hooks?

We store all ACF date field values as timestamps for improved flexibility. Thus we’d need to adjust how AC is handling this as well.

For now, we only managed to adjust the date display in the column like this:

// show correct date in columns view due to acf date values being stored as timestamps
add_filter( 'ac/column/value', function ( $value, $id, $column ) {

	if( get_post_type($id) !== 'event' ) {
		return $value;
	}

	if ( $column instanceof ACA\ACF\Column ) {
		$acf_field = $column->get_acf_field(); // Gets an ACF object
		$acf_type =  $column->get_acf_field_option( 'type' ); // Get the ACF field type
		if( 'date_time_picker' === $acf_type ) {		
			$value = date_i18n( $acf_field['display_format'], get_field($acf_field['key'], $id, false) );
		}
	}

	return $value;

}, 10, 3 );

Note the additional ACF get_field() query since the $value param has no valid value. BTW: Is there still some kind of ‘raw’ filter hook to get the exact value from the database here as in version 3?

Thanks in advance,
Daniel

4 months, 2 weeks ago
Daniel

ok, at least I found the $column->get_raw_value($id) hint in the docs to replace the get_field() call (even though I don’t know if this makes any difference other than using the plugins’ data model for consistency).

4 months, 2 weeks ago
Stefan
Developer

Hi Daniel,

There is no easy workaround to alter the logic for filtering, especially for the ACF date column.
It’s not possible to change the behavior of our so-called filtering models on the fly.
The ACF data filter looks for the ACF settings to alter the WP_Query. You probably alter the ACF value with a hook, which makes it quite impossible for our plugin to detect.

So, unfortunately, I don’t see a workaround for you to make filtering work on your altered ACF date field.
$column->get_raw_value( $id ) is indeed the method to retrieve the raw_value but this has nothing to do with filtering, since we don’t look at the actual column values, rather than the actual database values.

4 months, 2 weeks ago
Daniel

Hey Stefan,

thanks for your reply.

We change the specific ACF field values like this, just for reference:

add_filter('acf/update_value/type=date_time_picker', function ( $value, $post_id, $field ) {

	return strtotime($value);

}, 10, 3);

Sad to hear that it’s currently not possible, though.
But how about adding a filter hook to make the values that your filter model get’s from the DB more flexible. I mean if it’s possible to change the ACF values for storage, why not add a corresponding possibility for Admin Columns filter feature?
Or how exactly is Admin Columns getting values from ACF fields for filtering? Does it rely on ACF’s own API? If so, maybe there’s a way already…

4 months, 2 weeks ago
Stefan
Developer

We don’t retrieve the value for filtering.
We work directly on the database manipulating the SQL or WP_Query.
For our date filtering model, we cast any input date to a workable format.
The ACF DateTime picker is stored in a default format ‘Y-m-d H:i:s’.

ac-addon-acf/classes/Filtering/DateTimePicker.php (For default filtering)
ac-addon-acf/classes/Search/DateTimePicker (for Smart Filtering)

For you, the best thing to do is to create your own column using the following documentation

Creating a new column type

You could create an Abstract column, just for ACF DateTime fields and give them a specific Smart Filtering model, just like the ACF DateTime model and just change the format to a timestamp. You could have a look at one of all our column on how to attach a search model to a column since this part is not (yet) included in the toolkit

4 months, 1 week ago
Daniel

Thanks Stefan for pointing this out.

I’d still recommend to add some filter hooks in order to be able to manipulate those values, but I’m ok with this approach. This definitely helps with estimating the effort for it.

4 months, 1 week ago

You must be logged in to reply to this topic.