Home Forums ACF PRO Taxonomy loop not pulling all assigned objects


Taxonomy loop not pulling all assigned objects

  • I have a custom post type object assigned to 2 custom taxonomies. So it is a single attorney that says he is located at 2 offices. On the contact page, I made a WP_Query loop to show all of the attorneys at that single location. It is pulling all of the attorneys that have a single office location but not the people that are assigned to both locations.

    On the contact page, I made a field group with a taxonomy field to send to the WP_Query taxonomy argument. I think it has somethign to do with the get_field passing a single slug versus an array of slugs.

    And the strange thing is that one attorney shows in both like he is supposed to. That is the most confusing part. There are 17 attorneys and 5 of them are at both locations. But only 1 of those 5 shows up in both loops on both contact pages.

    Here is my loop.

    // Show child pages as menu items above associated attorneys
    function vbmmp_attorney_list_for_location() {
    	global $post;
    	global $query_args;
       	$location_tax = get_field('vbmmp_office_attorney_filter');
        $args = array(
            'post_type'         => 'attorney', 
            'numberposts'       => -1 ,
            'meta_key'          => 'name_details_last_name',
            'orderby'           => 'meta_value',
            'order'             => 'ASC',
            'tax_query' => array(
    				'taxonomy' => 'location',
    				'field'    => 'slug',
    				'terms'    => array($location_tax->slug),
        $query = new WP_Query($args);
        if ( $query->have_posts() ) {
    	    echo '<section class="widget">'; 
    	        echo '<div class="widget-wrap">';
    				echo '<h4 class="widget-title widgetitle">Attorneys</h4>';
    				echo '<table class="container">';
    					while( $query->have_posts() ) {
    						$attorney_phone 	= get_field('vbmmp_attorney_phone', $post->ID);
    						$attorney_email 	= antispambot(get_field('vbmmp_attorney_email', $post->ID));
    						echo '<tr class="menu-items">';
    						  echo '<td class="first one-third menu-item"><a href="'.get_the_permalink().'">'.get_the_title().'</a></td>';
    						  echo '<td class="one-third menu-item"><i class="icon ion-ios-telephone-outline" aria-hidden="true"></i> '.$attorney_phone.'</td>';
    						  echo '<td class="one-third menu-item"><i class="icon ion-ios-email-outline" aria-hidden="true"></i> <a href="mailto:'.$attorney_email.'">email</a></td>';
    						echo '</tr>';
    				echo '</table>';
    	        echo '</div>';
    	    echo '</section>';  
  • I might be a little confused about your setup from your descriptions. Just want to ask a couple of questions to make sure I understand.

    You have a custom field named vbmmp_office_attorney_filter

    This custom field is on either a page template or some other location and when you edit the page you select one of the terms from the location taxonomy.

    In the template you want to get the posts from attorney post type that also have the same terms selected.

  • John, you are correct. The custom field is on a custom post type object page template, office location. When I edit that CPT page, I select the taxonomy it relates to.

    Taxonomy -> location -> Dallas, McKinney, etc

    Custom Post Type -> office -> Dallas, McKinney, etc.

    Custom Post Type -> attorney -> can select a location from the taxonomy above

    I want to show all attorneys with a certain location taxonomy on a office page.

    Like you said in your description, in the edit screen for the office page, I select a location taxonomy and pass it to my loop on the office template.

    It shows the attorneys that only have one location taxonomy selected but does not show the attorneys that has both office locations selected.

  • Hey John, I figured this one out. I had numberposts -1 in my query. I changed it to posts_per_page -1 and everything is working fine now. Thanks for looking at it though!

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Taxonomy loop not pulling all assigned objects’ is closed to new replies.