Hi All,
I a trying to output the results of a relationship field. The issue I am having is I want to output the results grouped by CPT. I have searched all over and can’t quite get it to work. Below id the code I am using to group by post type, in a loop and store them in a PHP associative array.
$array = get_field('test_field');
$group = array();
foreach ( $array as $post ) {
$group[$post->post_type][] = $post;
}
Here is the var_dump of $group…
array (size=5)
'places' =>
array (size=3)
0 =>
object(WP_Post)[12566]
public 'ID' => int 728
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2020-01-16 03:22:25' (length=19)
public 'post_date_gmt' => string '2020-01-16 03:22:25' (length=19)
public 'post_content' => string '' (length=0)
public 'post_title' => string 'Clarendon Bakery' (length=16)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'closed' (length=6)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'clarendon-bakery' (length=16)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2020-01-16 23:11:29' (length=19)
public 'post_modified_gmt' => string '2020-01-16 23:11:29' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://localhost:10005/?post_type=places&p=728' (length=51)
public 'menu_order' => int 0
public 'post_type' => string 'places' (length=6)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
1 =>
object(WP_Post)[12543]
public 'ID' => int 944
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2020-09-16 04:31:46' (length=19)
public 'post_date_gmt' => string '2020-09-16 04:31:46' (length=19)
public 'post_content' => string '' (length=0)
public 'post_title' => string 'Oddfellows Hall' (length=15)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'closed' (length=6)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'oddfellows-hall' (length=15)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2020-09-17 01:15:47' (length=19)
public 'post_modified_gmt' => string '2020-09-17 01:15:47' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://localhost:10005/?post_type=places&p=944' (length=51)
public 'menu_order' => int 0
public 'post_type' => string 'places' (length=6)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
2 =>
object(WP_Post)[12542]
public 'ID' => int 943
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2020-09-16 04:31:32' (length=19)
public 'post_date_gmt' => string '2020-09-16 04:31:32' (length=19)
public 'post_content' => string '' (length=0)
public 'post_title' => string 'Wesleyan Church' (length=15)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'closed' (length=6)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'wesleyan-church' (length=15)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2020-09-16 04:31:32' (length=19)
public 'post_modified_gmt' => string '2020-09-16 04:31:32' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://localhost:10005/?post_type=places&p=943' (length=51)
public 'menu_order' => int 0
public 'post_type' => string 'places' (length=6)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
'people' =>
array (size=1)
0 =>
object(WP_Post)[12541]
public 'ID' => int 737
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2020-01-18 12:17:28' (length=19)
public 'post_date_gmt' => string '2020-01-18 12:17:28' (length=19)
public 'post_content' => string '<strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popular.
<strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has b'... (length=1197)
public 'post_title' => string 'Thomas Shipway' (length=14)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'closed' (length=6)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'thomas-shipway' (length=14)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2020-01-18 12:20:42' (length=19)
public 'post_modified_gmt' => string '2020-01-18 12:20:42' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://localhost:10005/?post_type=people&p=737' (length=51)
public 'menu_order' => int 0
public 'post_type' => string 'people' (length=6)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
'collection' =>
array (size=1)
0 =>
object(WP_Post)[12430]
public 'ID' => int 567
public 'post_author' => string '2' (length=1)
public 'post_date' => string '2019-12-03 01:26:30' (length=19)
public 'post_date_gmt' => string '2019-12-03 01:26:30' (length=19)
public 'post_content' => string '' (length=0)
public 'post_title' => string 'Photograph Pipeline' (length=19)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'closed' (length=6)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'cm-2019-0010' (length=12)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2021-03-04 11:25:06' (length=19)
public 'post_modified_gmt' => string '2021-03-04 11:25:06' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://history.clarendon.org.au/?post_type=collection&p=567' (length=64)
public 'menu_order' => int 0
public 'post_type' => string 'collection' (length=10)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
'things' =>
array (size=1)
0 =>
object(WP_Post)[12544]
public 'ID' => int 995
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2021-03-04 10:39:24' (length=19)
public 'post_date_gmt' => string '2021-03-04 10:39:24' (length=19)
public 'post_content' => string '<span style="font-weight: 400;">The construction of the Clarendon Weir and Happy Valley Reservoir in the 1890s to dam the Onkaparinga River and divert water to Adelaide was a significant engineering success.</span>
<span style="font-weight: 400;">A Royal Commission in 1888 addressed the increasing consumption of water in Adelaide due to the growing population and the establishment of Australia’s first water-based sewerage system in the city. The proposal of the Happy Valley Water Works including the co'... (length=4789)
public 'post_title' => string 'Clarendon Weir and Pipeline' (length=27)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'closed' (length=6)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'clarendon-weir-and-pipeline' (length=27)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2021-03-04 11:16:12' (length=19)
public 'post_modified_gmt' => string '2021-03-04 11:16:12' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://localhost:10005/?post_type=things&p=995' (length=51)
public 'menu_order' => int 0
public 'post_type' => string 'things' (length=6)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
'story' =>
array (size=1)
0 =>
object(WP_Post)[12567]
public 'ID' => int 725
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2020-01-16 02:53:02' (length=19)
public 'post_date_gmt' => string '2020-01-16 02:53:02' (length=19)
public 'post_content' => string 'Our bridge has an interesting history: connecting the north and the south it provided the only way to cross the Onkaparinga River for many miles around.
During WW2 it was thought that any enemy invasion would come from the south. If this occurred the bridge would need to be blown up. Holes were bored and charges laid and a ladder attached to the bridge to provide quick access. Guards were stationed at each end of the bridge in sentry boxes.
[caption id="attachment_803" align="aligncenter" width="919"]'... (length=2940)
public 'post_title' => string 'War & Peace' (length=11)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'closed' (length=6)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'war-peace' (length=9)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2021-07-04 11:27:54' (length=19)
public 'post_modified_gmt' => string '2021-07-04 11:27:54' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://localhost:10005/?post_type=story&p=725' (length=50)
public 'menu_order' => int 0
public 'post_type' => string 'story' (length=5)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
I am not sure how to start the next loop to get these arrays into grouped CPT display lists?
foreach ($group as $post_type => $posts) {
foreach ($posts as $post) {
}
}
Thanks for your reply John.
I can’t seem to get it to work. Was your code to fully replace what I had?
Here is what I am trying to format the results of the relationship field…
CPT 1
– result 1
– result 3
– result 4
CPT 2
– result 2
– result 6
CPT 3
– result 5
etc etc..
But because the user may pick the CPT in random order the results are grouped based on the order selected in the relationship field…
CPT 1
– result 1
CPT 2
– result 2
CPT 1
– result 3
– result 4
CPT 3
– result 5
CPT 2
– result 6
Hope this makes sense?
No, you asked
I am not sure how to start the next loop to get these arrays into grouped CPT display lists?
That is how you would loop over the group array and the nested posts in each group
You are right, thanks again John for your expertise đŸ™‚ Works perfectly
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.