Ashley Cameron Design

Ashley Cameron Design

Shop the Creative Design Market

Good morning

Advanced Custom Fields – Using field group ID and relationships to query multiple fields

This post was last updated: Sep 3, 2020
SnippetsWordPress

About a 1 minute read

"Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning."

Albert Einstein


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<ul>
 <?php
    $model = $post->post_name;
    query_posts(array('category_name' => $model));
    $features = $page->ID;
   
    foreach(get_field('relation', $features) as $relationship): ?>
        <li><strong>Model:</strong> <?php echo get_the_title($relationship->ID); ?></li>
        <?php $groupId = 232; //ID of field group. Get this from the Edit Field Group page url
            if (class_exists('Acf')) {
                $specs = new Acf();
                if (method_exists( $specs, 'get_acf_fields') ) {
                foreach ($specs -> get_acf_fields($groupId) as $field) {
                    $value = get_field($field['name'], $relationship->ID); ?>
                    <?php if ($value) { ?>
                        <li><strong><?php echo $field['label'] ?>:</strong> <?php echo $value ?></li>
                    <?php } ?>  
    <?php } } } ?>                                          
    <?php endforeach; ?>
    <?php wp_reset_query(); ?>
</ul>