Home › Forums › General Issues › Query/search terms of relationship
Hi folks! I have a Sales Rep CPT and an Areas CPT. Sales Rep has a relationship with Areas, Areas has the taxonomy Zip Codes. I’d like to set up a search bar to find a Sales Rep for an Area by Zip Code. For example, Nick is assigned to Vermont, so searching “05401” returns Nick as the result. Here is a better illustration of my setup: https://docs.google.com/drawings/d/11imHuKgLxLzPU7XFzww6InelvynQR-ZsKbnzy-r24QY/edit
Here is my code that currently displays all Sales Reps, their assigned areas and the zip codes for that area:
<?php
// the query
$find_a_rep = new WP_Query(array(
'post_type'=>'sales_reps',
'post_status'=>'publish',
'posts_per_page'=>-1
));
?>
<?php if ( $find_a_rep->have_posts() ) : ?>
<?php while ( $find_a_rep->have_posts() ) : $find_a_rep->the_post(); ?>
<div class="col-sm-6 card-wrap">
<div class="card">
<?php $image = get_field( 'image' ); ?>
<?php if ( $image ) { ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php } ?>
<div class="card-body">
<h5 class="card-title"><?php the_title(); ?></h5>
<p class="card-text">
Phone: <?php the_field( 'phone' ); ?><br>
Email: <?php the_field( 'email' ); ?><br>
<div class="areas">
Areas:<br>
<!-- GET AREAS FOR REP -->
<?php $rep_area_relationship = get_field( 'rep_area_relationship' ); ?>
<?php if ( $rep_area_relationship ): ?>
<?php foreach ( $rep_area_relationship as $post ): ?>
<div class="area">
<?php setup_postdata ( $post ); ?>
<?php the_title(); ?>
<!-- GET ZIP CODES FOR AREA -->
<?php $zip_code_terms = get_field( 'zip_codes' ); ?>
<?php if ( $zip_code_terms ): ?>
<?php foreach ( $zip_code_terms as $zip_code_term ): ?>
<li><?php echo $zip_code_term->name; ?></li>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
</p>
</div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no sales reps matched your criteria.' ); ?></p>
<?php endif; ?>
Thank you for taking a look!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.