thar
thar

Reputation: 1104

Wordpress, Advance custom fields: Fetch data from relationship field type

I'am developing a Wordpress site for an travel agency.

And for this project i'am using custom post types and advanced custom fields for travel-

types(business travel, group travel, school travel etc.) and employees. Each of this travel-types need a

contact person, which should be an employee. So I have a relationship field type in the custom post type

for travel-types, so I can set an employee, to be contact-person for an travel-type.

The question/problem is:

How can I fetch data from the relationship field employess? I thought this relationship field was

working like an INNER JOIN, but it's not? So... now I can only display all the information form travel-

types, but i can't figurer out, how to display the data for the related employee.

Thanks for your time, and thanks in advance

Troels

Upvotes: 0

Views: 875

Answers (2)

thar
thar

Reputation: 1104

I found an solution my self, and sharing it here for some one else to use. The following codes should be placed inside the Wordpress loop.

                <?php $fields = get_field('relationship_field_name'); ?>

                <?php if( $fields ): ?>
                    <?php foreach( $fields as $field ): ?>

                        <?php $name = get_field('field_name', $field->ID); ?>
                        <?php $image = get_field('field_name', $field->ID); ?>
                        <?php $email = get_field('field_name', $field->ID); ?>
                        <?php $phone = get_field('field_name', $field->ID); ?>

                        <h4><span><?php echo $name; ?></span></h4>

                        <img style="width: 150px; height: auto;" src="<?php echo $image; ?>" alt="<?php echo $name; ?>"/>

                        <ul class="fa-ul pad10">
                            <li><i class="fa-li fa fa-phone colour"></i><a href="tel:0045<?php echo str_replace(' ', '', $phone); ?>">+ 45 <?php echo $phone; ?></a></li>
                            <li><i class="fa-li fa fa-envelope colour"></i><?php echo $email; ?></li>
                        </ul>

                        <div class="pad10"></div>

                    <?php endforeach; ?>
                <?php endif; ?>

Upvotes: 1

Fatih SARI
Fatih SARI

Reputation: 433

I understand, you want to set connection two different post types. You can this with custom fields. Create a selectbox and there options is post of your other custom post type. For be easy you can use Rilwis's meta box plugin (https://github.com/rilwis/meta-box).

Your option value must post id. If you want get selected post, can use this:

$getIdWithField = get_post_meta($post->ID, 'custom_personel_field', true);
$post = get_post($getIdWithField);

Upvotes: 0

Related Questions