Support

Account

Home Forums General Issues Post Object Inside Loop Not Working

Helping

Post Object Inside Loop Not Working

  • Hi,

    Is anyone able to help?

    I have a standard WP loop for a custom post type (consultant) and within that there is a post object for a field that is another custom post type (company).

    Here is the code;

    <?php
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    if ( $query->have_posts() )
    {
    	?>
    	
    	<?php
    	while ($query->have_posts())
    	{
    		$query->the_post();
    		
    		?>
    
    		<div class="grid-container consultant-block p50all">
                    
                    <div class="grid-x grid-margin-x consultant-block-inner">
                        
                        <div class="cell small-12 medium-4 large-4 consultant-block-inner-left">
                            
                           <?php the_post_thumbnail(); ?>
                            
                        </div>
                        
                        <div class="cell small-12 medium-8 large-8 consultant-block-inner-right">
                            
                            <h3><?php the_field('prefix'); ?> <?php the_title(); ?></h3>
                            
                            <p><strong><?php the_field('qualifications_honours'); ?><br /><?php $cat_array = ( get_the_terms( get_the_ID(), 'specialty') ); $cat_str = array(); foreach ($cat_array as $cat) { $cat_str[] = $cat->name; } echo implode(", ",$cat_str) . ''; ?></strong></p>
                            
                            
                            <?php $company = get_field('company'); ?><?php if( $company ): ?><?php $post = $company; setup_postdata( $post ); ?>
                            
                            <p><a href="<?php the_field('website'); ?>">test</a></p>
                            
                            <?php wp_reset_postdata(); ?><?php endif; ?>
                            
                            <a class="button" href="<?php the_permalink(); ?>">View Profile</a>
                            
                        </div>
                    
                    </div>
                    
                    <hr>
                
                </div>
    
    		<?php
    	}
    	?>
    	
    
    	<?php
    }
    else
    {
    
    }
    ?>

    And here is just the ACF post object code;

    <?php $company = get_field('company'); ?><?php if( $company ): ?><?php $post = $company; setup_postdata( $post ); ?>
                            
                            <p><a href="<?php the_field('website'); ?>">test</a></p>
                            
                            <?php wp_reset_postdata(); ?><?php endif; ?>

    The initial loop works and pulls in the ‘consultant’ posts and their data, but the post object for the ‘company’ is not working at all.

    An example/test page is at http://litfieldhouse-co-uk.flexxstaging.com/patients/our-services/

    Some of the consultants have ‘test’ in their block which should be pulling the ‘website’ field from the ‘company’ post type. Instead, it just seems to be grabbing the URL of the current page.

    What am I doing wrong?

  • Edit – I ended up switching the company post type to a taxonomy as it seemed easier.

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

You must be logged in to reply to this topic.