Home › Forums › Front-end Issues › get_field_object return false
Hi everybody,
I’m using the function get_field_object in a custom post type archive for filter some parameters.
I’ve followed this tutorial: https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter
On the archive-cars.php everything it’s working fine. When I try to use the same code on a category field I get a warning because the $field = get_field_object($key, false, true);
doesn’t get any result but return false..
Do you know Why?
“Ivalid argument supplied for foreach() in /Applications/MAMP/htdocs/www.rr1985.localhost/wp-content/themes/rr1985/template-parts/archive-cars-filter.php on line 29”
Thanks
<?php foreach( $GLOBALS['my_query_filters'] as $key => $name ):
// get the field's settings without attempting to load a value
$field = get_field_object($key, false, true);
// create filter ?>
<h3><b><?php echo $name; ?></b></h3>
<div class="filter" data-filter="<?php echo $name; ?>">
<?php //Print the list in a checbox
if ($field) :
// BUG: funziona solo se trova risultati o nell'url iniziale
foreach ($field['choices'] as $choice_value => $choice_label) :
$checkAtt = '';
// set value if available
if( isset($_GET[ $name ]) ) {
$values = explode(',', $_GET[ $name ]);
if (in_array($choice_value, $values, false) ){
$checkAtt = "checked";
}
}
?>
<p>
<label>
<input name="brand" type="checkbox" value="<?php echo $choice_value;?>" <?php echo $checkAtt; ?>/>
<span><?php echo $choice_label; ?></span>
</label>
</p>
<?php endforeach;?>
<?php endif;?>
</div>
<?php endforeach; ?>
The reason is likely that a value has never been saved for this field for the current post and you are using the field name in $key
. Under this condition you need to use the field key instead of the field name.
You can solve this by building an array like this
$fields = array(
// field name => key pairs
'name-of-field-1' => 'field_123456',
'name-of-field-2' => 'field_234567',
// etc...
)
Then use
$field = get_field_object($field[$key], false, true);
Thanks a lot John for the reply.
I saved the value in a global array on my functions.php (or at least I think so..)
Any clue why on the archive-cars.php is working and in the category.php isn’t?
Here my functions.php file
**
* Archive cars FILTER using pre get post?
*/
// array of filters
//NOTE: it's a sub field inside a field group so the meta query becomes => groupname_subfield
$GLOBALS['my_query_filters'] = array(
'car_info_petrol' => 'carburante',
'car_info_gearshift' => 'cambio',
'car_info_brand' => 'marca'
);
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {return $query;}
// only modify queries for 'cars' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'cars' ) {
// loop over filters
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
//allow the url to alter the query
if( isset($_GET[$name]) ) {
$meta_query[] = array(
'key' => $key,
'value' => $_GET[$name],
'compare' => 'IN'
);
//Update the meta query args
$query->set('meta_query', $meta_query);
}
}
} //End of the if only on the post type cars
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
I don’t know why it might work in one location and not in another. The only thing that I do know is that get_field_object() will fail if the field has never been set and you use the field name but it should work when using the field key.
The topic ‘get_field_object return false’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.