0

i’ve readed doc about relation field for ACF, and it seems to be exactly what i need. Perhaps, i’m not able to get it work on front end. Maybe i’m doing something wrong ?

my process :

i have a travel guide site, and i wish to link custom posts to a city.

So i have created custom posts :

  • hotel
  • camping
  • bnb

PLUS

  • a custom post called "region", where i add the different cities.

In my custom post (except region) i added a relation field, linked to the "region" custom post.

So in each of my cpt hotels, campings, etc… i wish to display :

“hotel located in Paris”

and in my region pages, i want to display “all the hotels located in Paris”.

i’ve tried both code examples for single-doctor.php & single-location.php i can not have any data displayed. For single doctor, i even have an error message.

# my single location

in my single location, i’m tring to display all the markets of this city ( market CPT type is "marche".

so i call a function called : be_list_market_in_city()

and my function is :

function be_list_market_in_city() {

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


        $query = new WP_Query($args);
        if ($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
            $title = get_the_title();
            echo $title. '<br />';
        endwhile;
        wp_reset_postdata();
        endif;
    }

As you can see, i echo get_the_id() to be sure i have the good ID, and on front, i do have ID 1112 displayed ( 1112 is the good ID for the location displayed )

But i have no results returned. If i take of the meta_query filter, i do have all the market listed.

What am i doing wrong ?

#my single hotel

in my single hotel, i just made a try with the code specified in the doc, & changing my CPT

                $locations = get_field('contact_city');
                ?>
                <?php if( $locations ): ?>
                    <ul>
                    <?php foreach( $locations as $location ): ?>
                        <li>
                            <a href="<?php echo get_permalink( $location->ID ); ?>">
                                <?php echo get_the_title( $location->ID ); ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                    </ul>
                <?php endif; ?>

And this return :

Warning: Invalid argument supplied for foreach() in /home/xxx/zz_dns/xxx/wp-content/themes/xxx/template-parts/content-hotelsdetail.php on line 127

if i var_dump $location it returns :

string(56) “https://url-of-actual-loaded-page”

ps : i also tried with adding $term = get_queried_object(); & get_field('contact_city',$term); but it didn't change anything.

pps : here is my settings for the "contact_city" field

enter image description here

Thanks in advance for your precious help !

  • i'm turning around & around, and i don't see whats wrong. I really appreciate any ideas. – Gregory Mar 9 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.