0

I have a website which is about local types of foods and drinks. On the website, businesses can create a user and via the relationship field - add foods and drinks they offer.

The relationship field 'resturant' is displayed at /wp-admin/profile.php for logged in users.

Foods and drinks pages:

website.com/food/
website.com/food/cherry-pie/
website.com/food/pancakes/
website.com/food/cookies/
website.com/drinks/
website.com/drinks/lemonade/
website.com/drinks/soda/
website.com/drinks/water/

Three registered users:

website.com/author/resturant-cuppa-joes/
website.com/author/resturant-mclovin/
website.com/author/resturant-hotcup/

User Resturant Cuppa Joe's and Resturant MClovin have for example added Cherry Pie as one of the meals they offer. User Resturant Hotcup have not. When someone visits the page website.com/food/cherry-pie/ I want Resturant Cuppa Joe's and Resturant MClovin to be featured there, and not Resturant Hotcup.

In page.php I have the following code:

<?php while ( have_posts() ) : the_post(); ?>

            <h2>Resturants</h2>
            <?php 

            $resturants = get_users(array( // Changed from get_posts
                'meta_query' => array(
                    array(
                        'key' => 'resturant', // name of custom field
                        'value' => '"' . get_the_ID() . '"',
                        'compare' => 'LIKE'
                    )
                )
            ));

            ?>
            <?php if( $resturants ): ?>
                <ul>
                <?php foreach( $resturants as $resturant ): ?>
                    <li>
                        <a href="<?php echo get_permalink( $resturant->ID ); ?>"><?php echo get_the_title( $resturant->ID ); ?></a>
                    </li>
                <?php endforeach; ?>
                </ul>
            <?php endif; ?>
<?php endwhile; ?>

Used the sample at advancedcustomfields.com, changed get_posts to get_users.

Something seems to be right - I get a list with two links, but the links point to the start page.

What am I doing wrong?

2

The users doesn't have permalinks nor titles. They have author links and display names. Changed the code:

            <?php while ( have_posts() ) : the_post(); ?>

                        <h2>Resturants</h2>
                        <?php 

                        $resturants = get_users(array( // Changed from get_posts
                            'fields' => 'all_with_meta', // All with meta to get also display_name
                            'meta_query' => array(
                                array(
                                    'key' => 'resturant', // name of custom field
                                    'value' => '"' . get_the_ID() . '"',
                                    'compare' => 'LIKE'
                                )
                            )
                        ));

                        ?>
                        <?php if( $resturants ): ?>
                            <ul>
                            <?php foreach( $resturants as $resturant ): ?>
                                <li>
                                    <a href="<?php echo get_author_posts_url( $resturant->ID ); ?>"><?php echo $resturant->display_name; ?></a>
                                </li>
                            <?php endforeach; ?>
                            </ul>
                        <?php endif; ?>
            <?php endwhile; ?>
  • Worked like a charm. – Liu Kang May 11 '15 at 21:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.