4

I have a custom post type "building" and another custom post type "architect". Building are related to architects via an ACF relationship field.

In the single-architect page I want to display a list of the buildings that are associated to that particular architect.

So far I have achieved this by doing the following:

$loop = new WP_Query( array( 'post_type' => 'building', 'paged' => $paged, 'posts_per_page' => -1 ) );
if ( $loop->have_posts() ) : ?>

    <ul>

        <?php while ( $loop->have_posts() ) : $loop->the_post();

            $building_name = get_the_title(); 
            $building_link = get_the_permalink();

            $architects = get_field('architect'); 
            if( $architects ): ?>
                <?php foreach( $architects as $post):?>
                    <?php setup_postdata($post);
                    if( get_the_title() == $architect_name ) { ?>
                        <li><a href="<?php echo $building_link; ?>"><?php echo $building_name ?></a></li>
                    <?php } ?>
                <?php endforeach; ?>

                <?php wp_reset_postdata(); ?>
            <?php endif; ?>

        <?php endwhile; ?>

    </ul>

<?php endif;
wp_reset_postdata();

However this doese not seem very efficient and I am looking to introduce the relationship into the query itself, which I have tried by doing this:

$buildings = get_posts(array(
    'post_type' => 'building',
    'meta_query' => array(
        array(
            'key' => 'architect',
            'value' => '"' . get_the_ID() . '"',
            'compare' => 'LIKE'
        )
    )
));

<?php if( $buildings ): ?>
    <ul>
        <?php foreach( $buildings as $building): ?>

            <li><a href="<?php echo get_permalink( $building->ID ); ?>"><?php echo get_the_title( $building->ID ); ?></a></li>

        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Which is not working, it does not return anything...

Can you see what am I doing wrong or do you have any other idea to approach this situation?

Guillermo Carone
  • 818
  • 2
  • 9
  • 24
  • Where are you calling in the ACF field? I'm not seeing anything following traditional documentation patterns (https://www.advancedcustomfields.com/resources/relationship/) – Aibrean Feb 02 '17 at 21:52
  • Are you allowing multiple values for your architect field? If architect is a single select you're using the wrong meta query (the meta query above only applies to multi-selects because of the way data is stored). – Nathan Dawson Feb 03 '17 at 04:04
  • @NathanDawson architect is a relationship field which allows for multiple selects – Guillermo Carone Feb 03 '17 at 14:20
  • @Aibrean In my second example I was trying to follow the official ACF documentation as per this link https://www.advancedcustomfields.com/resources/querying-relationship-fields/ – Guillermo Carone Feb 03 '17 at 14:21
  • @GuillermoCarone did you find a solution ? ACF code is def not working as it should. – Rom Oct 14 '21 at 16:56
  • @Rom Hey! I just posted the code I'm currently using for this. Hope it helps. – Guillermo Carone Oct 15 '21 at 17:32
  • @GuillermoCarone Thanks ! I ended up reversing my system, but thanks any way, I'll probably come back here next time I have to use something like this. what is suppress_filters for ? Maybe that was the thing that was missing on my code. – Rom Oct 17 '21 at 14:11

3 Answers3

2

This is the solution that I'm currently using. I looks quite solid to me.

$buildings = new WP_Query(array(
    'post_type'        => 'building',
    'posts_per_page'   => -1,
    'suppress_filters' => 0,
    'meta_query'       => array(
            array(
                'key'      => 'architect',
                'value'    => '"'.$architect_id.'"',
                'compare'  => 'LIKE'
            )
        ),
        'order' => 'ASC',
        'orderby' => 'title',
        'post_status' => 'publish',
    )); 

while ( $buildings->have_posts() ) {
    $buildings->the_post();

    // print title, content, or whatever here.

}
Guillermo Carone
  • 818
  • 2
  • 9
  • 24
  • I am trying to do something similar but nothing from above is working for me. I get zero results and zero errors. In my case I have a "contacts" post type and a "locations" post type. When on a single "locations" page like, "columbus ohio", I want to list all the contacts that are associated with "columbus ohio" via the relationship field. I just can't seem to find a solution anywhere. Any idea what I may be doing wrong? – minemindmedia Jun 14 '22 at 19:32
  • @minemind I can confirm this code is still working for me with the latest ACF and WP versions, so your case comes down to debuging. Make sure you got the field 'key' right and that you are actually passing the ID in the 'value' field. If your relationship field is set to return the object then you'll need to make some operations in order to get the ID. – Guillermo Carone Jun 19 '22 at 19:27
1

Something like this should do it on the single architect page (because you're already in a loop to only display that architect and you need to get the buildings)...based on the ACF documentation.

<?php 

$posts = get_field('building');

if( $posts ): ?>
    <ul>
    <?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
        <li>
            <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
            <span>Custom field from $post: <?php the_field('author', $p->ID); ?></span>
        </li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

If you are basing it on a query, it would be more like

<?php 
$ids = get_field('building', false, false);

$query = new WP_Query(array(
    'post_type'         => 'architect',
    'posts_per_page'    => -1,
    'post__in'          => $ids,
    'post_status'       => 'any',
    'orderby'           => 'post__in',
));

?>

A lot of this is actually hinged on the way you're setting up the relationship. Are you having the custom field displayed on the architect page and selecting buildings there?

Aibrean
  • 6,297
  • 1
  • 22
  • 38
  • Thanks @Aibrean . I have a relationship field listing all architects show up in my "add building" page, so that when I add a new building I can link it with the corresponding architects. For what I can tell your code asumes the oposite, that buildings are selected inside the "add architect" page. Correct? – Guillermo Carone Feb 03 '17 at 21:45
  • You in theory could do both, but if you want buildings to display on the architect page, buildings have to be selected from that page. Otherwise using ACF is moot and you'd be better off with taxonomies. – Aibrean Feb 04 '17 at 15:56
0

I solved it by changing the value in the meta_query.

From this :

'"' . get_the_ID() . '"'

to :

get_the_ID()
buddemat
  • 4,552
  • 14
  • 29
  • 49
pASH
  • 1
  • 1
  • Damed, can you post your code ? It's driving me crasy. ACF code doesn't work. @pASH – Rom Oct 14 '21 at 16:55