Home › Forums › General Issues › Query posts by value (array?) and not by fieldname › Reply To: Query posts by value (array?) and not by fieldname
One step further…. this works for the first loop:
<?php
$posts = get_posts(array('meta_key' => 'Content'));
if( $posts ) {
$anzContent = array();
foreach( $posts as $post ) {
setup_postdata( $post );
$anzContent[] = get_field('Content');
}; //foreach( $posts as $post )
$uniqueContent = array_unique($anzContent);
foreach( $uniqueContent as $contentboxes ) {
setup_postdata( $contentboxes );
echo('<div class="Contentbox">');
echo($contentboxes);
echo('</div>'); //Contentbox
} //foreach( $uniqueContent as $contentboxes )
}; //if( $posts )
wp_reset_query();
?>
To get the Contentboxes i did 2 loops. One loop to get all Content values of all posts (like A,A,B,E,D,D,D).
then I put this array in an array_unique to get each value only once (A,B,E,D)
For each of this array_unique_values i createt a <div>.
This works so far.
Now i tried to get the wraps of each Contentbox the same way:
<?php
$posts = get_posts(array('meta_key' => 'Content'));
if( $posts ) {
$anzContent = array();
foreach( $posts as $post ) {
setup_postdata( $post );
$anzContent[] = get_field('Content');
}; //foreach( $posts as $post )
$uniqueContent = array_unique($anzContent);
foreach( $uniqueContent as $contentboxes ) {
setup_postdata( $contentboxes );
echo('<div class="Contentbox">');
$wraps = get_posts(array('meta_value' => $contentboxes));
if( $wraps ) {
$anzwraps = array();
foreach( $wraps as $wrap ) {
setup_postdata( $wrap );
$anzwraps[] = get_field('wrap');
}; //foreach( $wraps as $wrap )
$uniquewrap = array_unique($anzwraps);
foreach( $uniquewrap as $thiswrap ) {
setup_postdata( $thiswrap );
echo('<div>');
echo($thiswrap);
echo('</div>');
};
};
echo('</div>');
};
};
wp_reset_query();
?>
This is not working. It echoes the same value each contentbox. Why? I don’t get it… 🙁
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.