Support

Account

Home Forums General Issues Show posts that match Post Objects

Helping

Show posts that match Post Objects

  • Hi guys.
    So on my posts I have a Multiple Post objects field. In this field, you can select the pages you want the post to show on. This is done.

    So now I am trying to write the loop on the page.

    Step1: loop through posts, done. Shows all my posts.

    Step2 is where I am stuck, how do I then say “whilst going through each post check that this page ID is in the the post’s Post Objects field” if it is, then list this post.

  • I wrote this, it works by saving variables in a parent loop and using thin in a child loop that checks IDs using an if statement… it is not the cleanest solution, but it does work. If anyone has a better suggestion I am all ears

    
    <?php 
    $theID = $post->ID;
    
    $posts = get_posts(array(
    	'posts_per_page'	=> -1,
    	'post_type'			=> 'sas_news'	
    ));
    
    if( $posts ): ?>
    	
    
    		
    	<?php foreach( $posts as $post ): 	
    		setup_postdata( $post );
    		?>
    		<?php
    		$pn = get_the_title();
    		$pid = get_the_ID();
    		$purl = get_the_permalink();
    		$pex = get_the_excerpt();
    		$pimg = get_the_post_thumbnail_url($pid,'full'); 
    		?>
    
    		<?php $post_objects = get_field('link_to_sas_member'); ?>
    
    		<?php if( $post_objects ): ?>
       
        <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
    
    		
                <?php if($theID == get_the_ID($post_object->ID)  ){ ?>
    
    			<div class="post-item">
    				<div class="ptit">
    					<a href="<?php echo $purl; ?>"><h3>
    						<?php echo $pn; ?>
    						</h3></a>
    				</div>
    				<div class="pex">
    				<?php echo $pex; ?>
    				</div>
    			</div>
    
    			
    			
    <?php } ?>
           
        <?php endforeach; ?>
       
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>	
    		
    		
    		
    		
    <?php endforeach; ?>
    	
    	
    <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Show posts that match Post Objects’ is closed to new replies.