Home › Forums › Add-ons › Repeater Field › Repeater field returns string › Reply To: Repeater field returns string
Alright, so I have run into this thread numerous times and it never helped solve the issue.
I usually am writing my own plugin and using get_field seems to stop working with the identical behavior that @corydavidwilliam described. It’s not because of the theme or other plugins. It’s because of my own code. So I’ve isolated the problem and wanted to share, in case others run into the same.
The following code was the culprit, the cause of my repeater returning string “1” instead of array( ‘date’ => ‘9/20/2017’, ‘time’ => ’12:30pm’ ).
/**
* Sort open house in RSS FEEDS by open house date, and hide expired entries.
*
* @param $query
*/
function bn_open_house_sort_rss_by_open_house_date( $query ) {
if ( !is_feed() ) return;
if ( get_query_var('post_type') != 'open_house' ) return;
if ( !($query instanceof WP_Query) ) return;
$query->set('order', 'ASC');
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'open_house_date_timestamp');
$query->set('meta_query', array(
array(
'key' => 'open_house_date_timestamp',
'value' => strtotime('-6 Hours'), // Do not show posts from several hours earlier
'compare' => '>=',
'type' => 'NUMERIC',
)
));
}
add_action( 'pre_get_posts', 'bn_open_house_sort_rss_by_open_house_date' );
The problem with the above code is that get_query_var(‘post_type’) returns “open_house” during the loop of an RSS feed, as expected. However, get_field() performs it’s own query which tries to get the post type “acf-field” (the field group or specific field, I assume).
Because my code is adding a meta_query, the acf field query never returns a result. My meta query should only apply to open houses – not ACF fields!
The solution is to instead check the post type of the current query, and NOT use get_query_var.
Replace: if ( get_query_var(‘post_type’) != ‘open_house’ ) return;
With: if ( $query->get(‘post_type’) != ‘open_house’ ) return;
This makes it so I still have the same behavior as before, except it doesn’t interfere with the get_field() function.
Note that this won’t be an exact solution for everyone, but since this pops up so often I’m guessing people need to check into the pre_get_posts hook and see if that’s where the problem is coming from. I didn’t expect get_field() to perform a WP_Query.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.