Home › Forums › Add-ons › Flexible Content Field › Getting flexible content subfields after $wpdb->get_results($querystr);
After trying for hours I hope somebody can help me :/ I tried various snippets
I’m desperately trying to obtain an array of sub fields (of a flexible field for a custom post_type) on a custom archive page. Or any kind of solution to be able too loop through the flexible content subfield values.
I use $pageposts = $wpdb->get_results($querystr);
to get the post to display. (Need to use a custom SQL Query – need to build some more complex archive page queries that cannot be done with WP_Query).
Seems to work here: list-all-sub-fields-in-a-repeater-or-flex-field/
For a custom post type I defined various acf fields and one flexible content field called “dati” with subfields “dDatum, sSchiff,…” and can display the content of any field in my custom loop with for example the_field('mPreis');
But $rows = get_field("dati", $post->ID);
returns an array without subfields – just the layout name? ‘neues_datum’
<?php
$rows = get_field("dati", $post->ID);
print_r($rows);
/*
Output: Subfields"neues_datum" is the layout name
Array
(
[0] => neues_datum
[1] => neues_datum
[2] => neues_datum
[3] => neues_datum
[4] => neues_datum
[5] => neues_datum
[6] => neues_datum
[7] => neues_datum
[8] => neues_datum
[9] => neues_datum
[10] => neues_datum
[11] => neues_datum
[12] => neues_datum
)
*/
?>
Here is the full code of my current try with a simple query:
<?php
$querystr = "
SELECT * FROM wp_postmeta, wp_posts
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_type = 'themenfahrten'
AND wp_posts.post_status = 'publish'
AND wp_postmeta.meta_key LIKE 'dati_%_dDatum'
ORDER BY STR_TO_DATE(meta_value,'%d.%m.%Y') ASC";
global $wpdb;
$pageposts = $wpdb->get_results($querystr);
if ($pageposts):
foreach ($pageposts as $post) { ?>
<div class="">
<?php //Random Output for current post ?>
</div>
<?php
echo the_field('mPreis');
$rows = get_field('dati', $post->ID);
if (get_field('dati')):
//Someting todo here?
endif;
echo "<pre>";
print_r($rows);
echo "</pre>";
}
?>
<?php
else: ?>
<br>
<span class="uk-text-warning uk-text-large"><b><?php echo $noResults ?></b></span>
<?php
endif; ?>
It seems have_rows is not working within this kind of loop – I use this on single_posttype pages and it works without any problems:
<?php
if (have_rows('dati')):
while (have_rows('dati')):
the_row();
echo get_sub_field('dDatum');
echo get_sub_field('sAbfahrtsort');
//etc..
endwhile;
endif;
?>
I might add it works on all other pages within the normal wp loop.
Would really appreciate any kind of solution! 🙁
I suppose I need to get `have_rows’ to work within my loop?
I tried with setup_postdata(); within the loop – same result. And I shouldn’t it work with post ID only?
Thanks!
Edit: Just tried have_rows with current post->ID in the loop. Same result. Output is always “no rows”.
<?php
if (have_rows('dati', $post->ID)):
echo ":)";
while (have_rows('dati')) : the_row();
endwhile;
else :
echo "no rows";
endif;
?>
silly me – the query is the issue. no other meta_values are passed – so there are no rows. I will modify this archive with a ‘pre_get_posts’ action to modify the query.
thanks anyway 🙂
Unfortunately the issue wasn’t solved.
After trying everything I found that the problem applies only to tag pages.
No solution so far – same code works like a charm on archive pages.
In case anybody has a solution or idea why the flexible content/repeater fields don’t work on tag pages I’d appreciate it :/
The topic ‘Getting flexible content subfields after $wpdb->get_results($querystr);’ 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.