Comparing the title from one custom post type to the field of another CPT
-
I’m using Custom Post Type UI and Advanced Custom Fields plugins.
I have a CPT for states called ‘Locations’ (eg: Illinois, Iowa, Colorado, etc) and another CPT for cities called ‘Branches’ (eg: Chicago, Des Moines, Denver, etc).
I have single-locations.php for each of the locations pages, which displays the state name (wp_title), a map custom field tied to that state’s post, and then a listing of the appropriate cities/branches for that state, which I’m calling with a WP_Query (saved as a variable, $branchquery). As I’m running only one CPT for locations/states and only one for branches/cities, I need a way to make sure only the proper cities are displayed to their corresponding branch (by default all of them would show, and I don’t want that). What I did in the custom fields for Branches is created a ‘state’ text field that would be used to compare against the state that is currently loaded.
So what I did in the single-locations.php file, I stored get_the_title() in a variable (this is the state name) before I called the WP_Query for the ‘branch’ post-type. That variable is $pagestate. Then, once I start the loop for the $branchquery, I create another variable called $poststate that gets the ‘state’ text field I created for the Branch post type. Then, I begin an if statement to see if $pagestate == $poststate, basically seeing if the state assigned to the branch post matches the state that the current page is on (any branches with “Iowa” in the state field should display on the “Iowa” state page).
Here is the code on single-locations.php:
<div class="copy"> <?php the_field('state_map'); ?> <?php $pagestate = get_the_title(); $args = array( 'post_type' => 'branch', 'order' => 'ASC', 'orderby' => 'title' ); $branchquery = new WP_Query( $args ); ?> <ul> <?php if ( have_posts() ) : while ( $branchquery->have_posts() ) : $branchquery->the_post(); ?> <?php $poststate = get_field('state'); if( $pagestate == $poststate ): ?> <li> <ul> <li class="loc-branchname"><?php the_title(); ?></li> <li class="loc-streetaddress"><?php the_field('street'); ?></li> <li class="loc-citystatezip"><?php the_field('city_state_zip'); ?></li> <li class="loc-phone">Phone: <?php the_field('phone'); ?></li> <li class="loc-fax">Fax: <?php the_field('fax'); ?></li> <li class="loc-mainhours">Hours: <?php the_field('main_hours'); ?></li> <?php if ( get_field('alt_hours') != ""): ?> <li class="loc-althours"><?php the_field('alt_hours'); ?></li> <?php endif; ?> <?php if ( get_field('closed_on') != ""): ?> <li class="loc-closed"><?php the_field('closed_on'); ?></li> <?php endif; ?> <li class="loc-branchname"><a href="<?php the_field('job_opening_link'); ?>">Job Openings</a></li> </ul> </li> <?php endif; ?> <?php endwhile; endif; ?> </ul> </div>
So, in theory, this should work. I tested this by entering all of my Iowa branches, and then a single branch we have in New York, and it worked great, displaying all of the Iowa branches only, and displaying only the single New York branch on New York. But as I started adding more branches, some branches stopped showing. Initially all of my Iowa branches showed, but now only about three of them do. Some other states show no branches at all. The state names on the Location page and each of the branches match, so they should be displaying. But…they aren’t.
I’m at a loss. Apologies if this seems convoluted or confusing, but I’m in this fairly deep and really don’t have the time to go back to the drawing board. I don’t have a URL as I’m testing this locally yet. Let me know if there’s anything important I missed.
- The topic ‘Comparing the title from one custom post type to the field of another CPT’ is closed to new replies.