Hello! I’m using the following code to add fields to returned post objets.
It works perfectly fine, however if i add a post_object field inside a repeater, it causes internal server errors 🙁
function my_acf_format_value( $value, $post_id, $field ) {
if (is_array($value)) {
foreach ($value as $v) {
if (get_fields($v)) {
$v -> acf = get_fields($v);
}
if (get_the_category($v)) {
$v -> category = get_the_category($v);
}
if (get_permalink($v)) {
$v -> link = get_permalink($v);
}
}
}
else {
if (get_fields($value)) {
$value -> acf = get_fields($value);
}
if (get_the_category($value)) {
$value -> category = get_the_category($value);
}
if (get_permalink($value)) {
$value -> link = get_permalink($value);
}
}
return $value;
}
// need priority > 10 else type=post_object causes errors
add_filter('acf/format_value/type=post_object', 'my_acf_format_value', 100, 3);
Any idea what needs to be done for this to work inside repeaters?
Cheers
Calling get_fields() is likely creating an infinite loop.
Add this at the top of your function
remove_filter('acf/format_value/type=post_object', 'my_acf_format_value', 100);
and then add this before your return
add_filter('acf/format_value/type=post_object', 'my_acf_format_value', 100, 3);
Unfortunately this is still causing internal server errors 🙁
Tried commenting out the foreach loop, still crashes
Even simply trying to add fields for get_permalink or get_category causes it to crash
It does not crash when i add string fields
$v -> ‘test’ works
Im using wordpress as rest api so im not sure how to get details about the error
Wp debug mode set to true but doesnt record any error logs 😐
You must be logged in to reply to this topic.
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.