Forums › Forums › Search & Filter Pro › Problem filtering dates inserted as custom fields in ACF
Tagged: acf, custom fields, search by date range
- This topic has 5 replies, 1 voice, and was last updated 1 year, 2 months ago by Anonymous.
-
Anonymous(Private) July 13, 2017 at 9:17 am #120350
Hello,
I have a custom post type called projects that contains a custom field (inserted by the plugin ACF) that is a “date Picker”.
I want to filter these projects by date range but I am having trouble doing so as no results are being shown. I found other threads with similar problem and followed instructions but still didn’t manage to make it work.
Here I forward screenshots of the custom field configuration with ACF plugin and the Date Range configuration of the date range.
Could you please help me out? Thanks!
Trevor(Private) July 13, 2017 at 11:04 am #120379I am not certain what you are asking. If you want to know how to add a Custom Field search element to the form, drag and drop the Post Meta element in the form UI. When selecting the meta key (field name), for ACF make SURE that the name does NOT start with an underscore _
e.g.
_date_of_the_project_
is WRONG
date_of_the_project_
is CORRECTNote that the last underscore in the name suggests that you have a blank space at the end of the field label when you defined it.
Anonymous(Private) September 18, 2023 at 9:27 am #276863Howdy Trevor!
Instead of a range date picker, we’re gonna go with only one date picker.
The thing is we need to query from that date in advance, so we did with this code.
function filter_query_args( $query_args, $sfid ) { // Trainings Form ID. if( 1788 === $sfid && ! empty( $_GET['_sfm_start_date'] ) ) { $year = substr( $_GET['_sfm_start_date'], 4, 4 ); $month = substr( $_GET['_sfm_start_date'], 2, 2 ); $day = substr( $_GET['_sfm_start_date'], 0, 2 ); $fdate = $year . $month . $day; $query_args['posts_per_page'] = -1; $query_args['orderby'] = 'meta_value_num'; $query_args['meta_key'] = 'start_date'; $query_args['meta_value'] = $fdate; $query_args['meta_compare'] = '>='; $query_args['meta_query'] = [[ 'key' => 'start_date', 'value' => $fdate, 'compare' => '>', ]]; /*$q = new WP_Query( $query_args ); print '<pre><br><br>'; //print $fdate . '<br>'; print count( $q->posts ) . '<br>'; //print_r( $query_args ); //print_r( new WP_Query( $query_args ) ); foreach( $q->posts as $p ) print get_post_meta( $p->ID, 'start_date', true ) . ' : ' . $p->post_title . '<br>'; print '</pre>';*/ } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_query_args', 99, 2 );
We’ve modified the query correctly, or that says the debug (as you can see commented in the code). The data is there, but somehow the results are not showing.
Any hints? Thanks in advance.
Anonymous(Private) September 18, 2023 at 2:41 pm #276864Hi again!
BTW, trying another hook to keep progressing, we’ve encountered the same problem.
Here’s the code, adapted to query instead of query args, using the hook:
function filter_query_args( $query, $sfid ) { if( 1788 === $sfid && ! empty( $_GET['_sfm_start_date'] ) ) { $year = substr( $_GET['_sfm_start_date'], 4, 4 ); $month = substr( $_GET['_sfm_start_date'], 2, 2 ); $day = substr( $_GET['_sfm_start_date'], 0, 2 ); $fdate = $year . $month . $day; $query->post_type = 'product'; $query->posts_per_page = -1; $query->order = 'ASC'; $query->orderby = 'meta_value_num'; $query->meta_key = 'start_date'; $query->meta_value = $fdate; $query->meta_compare = '>='; $q = new WP_Query( $query ); print '<pre><br><br>'; print $fdate . '<br>'; print count( $q->posts ) . '<br>'; foreach( $q->posts as $p ) print ( get_post_meta( $p->ID, 'start_date', true ) ?: $p->post_date ) . ' : ' . $p->post_title . '<br>'; //print_r( $query ); print '</pre>'; } return $query; } add_filter( 'sf_main_query_pre_get_posts', 'filter_query_args', 99, 2 );
The commented code debug prints out the products filtered correctly and ordered within dates.
20230701 19 20230705 : POSTNAME 20230821 : POSTNAME 20230828 : POSTNAME 20231001 : POSTNAME
Looking forward for some tips and tricks.
-
AuthorPosts