1

I'm using Advanced Custom Fields and Genesis and trying to modify a template to reflect a relationship between two custom post types (using their associated custom fields.) In this case, I have custom post types for "Staff" and "Reports", and for staff, I have the custom fields "first_name" and "last_name"; and associated with the "Reports" CPT, I also have two custom fields "primary_contact" and "associated_contact"— and in the "single-staff.php" template, I'd like to render a list of any associated reports in the sidebar that would include entries (post titles) for which that staff member is defined as either a "primary_contact" or an "associated_contact."

Thus, for example, if I've created "Tom Jones" as a staff member (an instance of the "Staff" post type), is it possible for me to define a query that outputs a list of reports for which "Tom Jones" is defined as a "primary_contact" and/or "associated_contact" in any instances of the "Reports" custom post type? I'm including a (very) rough concept for this query below:

$args = array(
'meta_query' => array(
        'relation' => 'OR',
        array(
            'post_type' => 'reports',
            'meta_key' => 'primary_contact',
            'meta_value' => '$this.full_name',
        ),
        array(
            'post_type' => 'reports',
            'meta_key' => 'associated_contact',
            'meta_value' => '$this.full_name',
        )
    )
);

$query = new WP_Query( $args );

Please let me know if the question is not sufficiently clear-- and thank you for any insight!

2 Answers 2

2

I haven't tested this but it SHOULD work. You'll want to put it in the single-staff.php. You'll have to add the details (actual query, post types, etc).

$reports = get_posts(array(
                            'post_type' => 'reports',  //use actual post type
                            'meta_query' => array(
                                'relation' => 'or',
                                array(
                                    'key' => 'primary_contact', // name of custom field
                                    'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
                                    'compare' => 'LIKE'
                                )
                                array(
                                    'key' => 'associated_contact', // name of custom field
                                    'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
                                    'compare' => 'LIKE'
                                )
                            )
                        ));

                        ?>
                        <?php if( $reports ): ?>
                            <ul>
                            <?php foreach( $reports as $report ): ?>
                                <li>
                                    <?php echo get_the_title( $doctor->ID ); ?>
                                </li>
                            <?php endforeach; ?>
                            </ul>
                        <?php endif; ?>
6
  • thank you very much for the feedback @rudtek-- would you explain how/why you're using get_the_ID() to match the name of the relevant staff member? And the echo line would be get_the_title( $report->ID); no?
    – nickpish
    Commented Apr 4, 2017 at 5:02
  • oops yep. $report -> ID. I don't know your exact setup with fields etc, but (relationship, post object etc) so you may have to change a bit. See this example: advancedcustomfields.com/resources/querying-relationship-fields
    – rudtek
    Commented Apr 4, 2017 at 5:05
  • Ok thanks-- in my case the main fields are all post objects, e.g. "primary_contact" and "associated_contact" are defined in ACF as post objects with the relevant filter applied, just as with the "reports" CPT, the related contacts are defined as post objects with the filter "staff" applied. All are configured to return post IDs, so I'm thinking it will work with your approach?
    – nickpish
    Commented Apr 4, 2017 at 5:15
  • should. I'm on pins and needles waiting to find out!
    – rudtek
    Commented Apr 4, 2017 at 5:15
  • Ha ok, I'll try and report back!
    – nickpish
    Commented Apr 4, 2017 at 5:17
1

Another option: there's an ACF add-on called Bidirectional Relationships that may simplify this for you. You define relationships in ACF, and then when you're editing either post type, you'll see a box that lets you search or select from a list the related content. You can then use that info in your templates however you like.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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