• Anonymous User 13716511

    (@anonymized-13716511)


    I am trying to show all posts that have a specific post type (either ‘dog’ or ‘cat’) and have the field ‘virtual_adoption’ set to ‘1’.
    I’ve coded for the ‘dog’ and ‘cat’ in the same way. The ‘dog’ is working fine, but the ‘cat’ only bring the permalink and image for the page, not for the specific cat.
    It seems the ‘dog’ is ‘in the loop’ and the ‘cat’ not.
    Why isn’t it working the same for both of the post types? How do I get the ‘cat’ to bring the ‘cat’ permalink and image?

    <?php
    $posts = get_posts(array(
    	'post_type'=> 'dog',
    	'meta_query' => array(
    		array(
    		'key' => 'virtual_adoption',
    		'value' => '1',
    		'compare' => '=='
    		)
    	)
    ));
    $posts_c =  get_posts(array(
    	'post_type'=> 'cat',
    	'meta_query' => array(
    		array(
    		'key' => 'virtual_adoption_cats',
    		'value' => '1',
    		'compare' => '=='
    		)
    	)
    ));
    ?>
    
    	<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
    
    	<div style="display: table-row;" class="vrow">
        		<div style="display: table-cell;" class="virtual_image">
    				<?php if ( has_post_thumbnail()) the_post_thumbnail('pet-image'); ?>
    		</div>
      		<div style="display: table-cell;" class="virtual_name">
    			<a>"><?php echo "Sponsor "; echo the_title(); echo ':'; ?></a>
    			<span class="field_v_label"> <?php echo ' Age:'; ?> </span> <?php echo get_field( "age_range_dog" ); ?>
    			<span class="field_v_label"> <?php echo ' Information:'; ?> </span> <span class = "field_v_content"><?php the_content(); ?></span>
    		</div>
    	</div>	
    
    	<?php endforeach; ?>
    	<?php wp_reset_postdata(); ?>
    
    	<?php foreach( $posts_c as $post_c ): setup_postdata( $post_c ) ?>
    
    	<div style="display: table-row;" class="vrow">
        		<div style="display: table-cell;" class="virtual_image">
    				<?php if ( has_post_thumbnail()) the_post_thumbnail('pet-image'); ?>
    		</div>
      		<div style="display: table-cell;" class="virtual_name">
    			<a>"><?php echo "Sponsor "; echo the_title(); echo ':'; ?></a>
    			<span class="field_v_label"> <?php echo ' Age:'; ?> </span> <?php echo get_field( "age_range_cat" ); ?>
    			<span class="field_v_label"> <?php echo ' Information:'; ?> </span> <span class = "field_v_content"><?php the_content(); ?></span>
    		</div>
    	</div>	
    
    	<?php endforeach; ?>
    	<?php wp_reset_postdata(); ?>
    
    </div>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    To properly setup post data, you actually need to use (or re-use) the $post variable as the current post object. You also need to declare global $post; before the first loop starts.
    <?php foreach( $posts_c as $post ): setup_postdata( $post ) ?>

    Thread Starter Anonymous User 13716511

    (@anonymized-13716511)

    Thank you so much. That fixed it.

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

The topic ‘get_posts not returning correct image and permalink’ is closed to new replies.