Corneliatt
Corneliatt

Reputation: 65

How to link to ACF Relationship "parent"

I'm using Advanced Custom Fields in Wordpress to be able to relate content with each other. This is the scenario:

• Make a relationship between post type “design” and post type “item”

• On all of the “design” pages, I’d like to show all “items” from all designs.

• When I click on one of these “items”, id like to go to the url of the “design” linked to that specific item.

This is how it looks like right now:

 <?php $items = get_field('items'); ?>
 <?php foreach( $items as $item ): ?>
                            <?php setup_postdata($item); ?>


                            <?php 


                    $design = get_posts(array(
                        'post_type' => 'design',
                        'meta_query' => array(
                            array(
                                'key' => 'items', // name of custom field
                                'value' => '"' . $item->ID. '"',
                                'compare' => 'LIKE'
                            )
                        )
                    ));

                    ?>                      

<a href="<?php echo get_permalink( $design->ID ); ?>" target="blank">Link to "Design"</a>
<?php endforeach; ?>

I thought this would get the url of the related post type "Design", but all I get is the url to the current page.

Does anyone know what I'm doing wrong?

EDIT: The ACF Relationship tutorial has an example like this:

 $design = get_posts(array(
                        'post_type' => 'design',
                        'meta_query' => array(
                            array(
                                'key' => 'items', // name of custom field
                                'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
                                'compare' => 'LIKE'
                            )
                        )
                    ));

                    ?>
                    <?php if( $design ): ?>
                        <ul>
                        <?php foreach( $doctors as $doctor ): ?>

But instead of get_the_ID(), I first need to get the ID of each item, and then find the Design including that item. What do I need to change from the tutorial code?

Upvotes: 2

Views: 2278

Answers (1)

ztvmark
ztvmark

Reputation: 1434

You need see...

https://www.advancedcustomfields.com/resources/relationship/

and this for get "childs" https://www.advancedcustomfields.com/resources/querying-relationship-fields/

for get the id

https://wordpress.stackexchange.com/questions/58943/convert-post-name-into-post-id

maybe need this

replace get_the_ID by the $post->ID in this part.. 'value' => '"' . get_the_ID() . '"',

$post = get_page_by_title( 'your-slug', OBJECT, 'your-post-type' );
echo $post->ID;

note

-Don´t forget update your post

Upvotes: 1

Related Questions