• Resolved sreuber

    (@sreuber)


    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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter sreuber

    (@sreuber)

    Of course, after typing all of that, I figure it out.

    Just needed to add ‘posts_per_page’=> -1, to my args array so it would display everything.

    $args = array(
    	'post_type' => 'branch',
    	'order' => 'ASC',
    	'posts_per_page'=> -1,
    	'orderby' => 'title'
    );

    Hope this helps someone else, or if someone swings by and notices that I’m doing something terribly wrong here, I’m all ears.

    Moderator bcworkz

    (@bcworkz)

    You appear to not be using wp_reset_postdata() after running your custom WP_Query loop. You must do this! Your previous queries were inheriting random post per page settings because of not reseting the data. By explicitly setting the posts per page every time, you fixed that issue, but you will have other issues if you do not reset the post data.

    Thread Starter sreuber

    (@sreuber)

    What other issues will I have? After extensive testing it seems to be working as I need it to.

    Moderator bcworkz

    (@bcworkz)

    It’s difficult to say, side effects of not resetting are highly variable and random. Even if there is no visual evidence of other issues, having stale data leftover from your finished query as part of the original query cannot be a good thing, unless you are completely discarding the query anyway.

    I believe resetting the data as recommended is good practice whether doing so appears to have any effect or not. You may do as you like with your own site of course.

    Thread Starter sreuber

    (@sreuber)

    I’ll keep it in mind and see what I can do. This is my first WordPress-theme rodeo. Thanks for the tip.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Comparing the title from one custom post type to the field of another CPT’ is closed to new replies.