Home › Forums › Add-ons › Flexible Content Field › have_rows is returning false
Hi,
I’m using ACF and Algolia, it seems to do what I’m expecting, except for one thing :
the have_rows is returning false even tho there are rows, as you can see, as it doesn’t pass the have_rows : true, it goes to the false statement where I assign the field ($blockContentField) to $pageContent and then $pageContent is sent to algolia. I’m checking algolia and there is content. Not sure why have_rows return false at first place, it should be true.
I have seen other posts on the forum talking about this issue, but none with a real fixe at least none are working in my case. I’m using the flexible content and in the doc it says clearly to use have_rows.
Thank you
<?php
function algolia_post_to_record(WP_Post $post) {
$tags = array_map(function (WP_Term $term) {
return $term->name;
}, wp_get_post_terms($post->ID, 'tags'));
$fields = array_map(function (WP_Term $term) {
return $term->name;
}, wp_get_post_terms($post->ID, 'field'));
$sectors = array_map(function (WP_Term $term) {
return $term->name;
}, wp_get_post_terms($post->ID, 'sector'));
$pageContent = '';
$blockContentField = get_field('content_block', $post->ID );
if( have_rows($blockContentField) ):
$pageContent = 'item';
else :
// no rows found
$pageContent = $blockContentField;
endif;
return [
'objectID' => implode('#', [$post->post_type, $post->ID]),
'title' => $post->post_title,
'author' => [
'id' => $post->post_author,
'name' => get_user_by('ID', $post->post_author)->display_name,
],
'excerpt' => $post->post_excerpt,
'content' => strip_tags($post->post_content),
'tags' => $tags,
'fields' => $fields,
'sectors' => $sectors,
'url' => get_post_permalink($post->ID),
// 'pageContent' => get_field('content_block', $post->ID ),
'pageContent' => $pageContent,
];
}
add_filter('post_to_record', 'algolia_post_to_record');
I’m using last version of ACF
have_rows() is looking for a field name. You are using the return array of the field instead.
// use this
if (have_rows('content_block', $post->ID)):
// or this
if (!empty($blockContentField)):
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.