Support

Account

Home Forums General Issues Trying to display posts related to other posts

Solving

Trying to display posts related to other posts

  • I’m trying to loop each individual client (custom post type) to the related content. I’m using the relationship as my custom post type to make the association. But now I’m looking to generate a page where I can see the client name and the associated other posts. This is the code so far. I can’t seem to figure out out to make a for each client loop that separates out the associated posts that relate to each client.

    <?php 
    
    // get only first 3 results
    $clients = get_field('attach_to_client');
    
    $query = new WP_Query(array(
    	'post_type'      	=> 'any',
    	'posts_per_page'	=> 10,
    	'post__in'		=> $clients,
    	'post_status'		=> 'any',
    	'orderby'        	=> 'modified',
    ));
    
    ?>
    <h3><?php get_field( 'clients' ) ?></h3>	
    <?php
    if ($query->have_posts()) :
    	while($query->have_posts()) :
    		$query->the_post();
    ?>
    
    <ul>
    	<li><?php the_title(); ?></li>
    </ul>
    
    <?php
          endwhile;
       else: 
    ?>
    
          Oops, there are no posts.
    
    <?php
       endif;
    ?>

    The format I’m looking for a loop that does this as a foreach client:

    Client Name
    Associated posts titles

  • Couple things:

    1. On get_field in this manner you may need to pass two additional parameters according to the docs at http://www.advancedcustomfields.com/resources/relationship/
    2. Sometimes post_type => 'any' fails, so might be worth a shot with a set array just to test
    
    <?php 
    $ids = get_field('attach_to_client', false, false);
    
    $query = new WP_Query(array(
    	'post_type'      	=> array('post','page'),
    	'posts_per_page'	=> 10,
    	'post__in'		=> $ids,
    	'post_status'		=> 'any',
            'orderby'        	=> 'modified',
    ));
    ?>
    <h3><?php get_field( 'clients' ) ?></h3>	
    <?php if ($query->have_posts()) : ?>
    <ul>
    <?php
    	while($query->have_posts()) :
    		$query->the_post();
    ?>
    	<li><?php the_title(); ?></li>
    
    <?php endwhile; ?>
    </ul>
    <?php else: ?>
    
          <p>Oops, there are no posts.</p>
    
    <?php
       endif;
    ?>
    
  • Is there a way to add a foreach client loop around so that the posts separate out based on which custom post (clients) they are associated with?

  • Here is what I have so far. I’m able to see all the clients from my custom post type and all the posts attached to clients. However, I want it to only show the posts relating to a particular client. I’m not sure what I’m missing.

    The ‘attach_to_client’ is the relationship function to the custom post type of ‘clients’.

    <?php 
       $loop = new WP_Query( array( 'post_type' => 'clients', 'posts_per_page' => 10 ) );
       $ids = get_field('attach_to_client', false, false);
    
       $query = new WP_Query(array(
    	'post_type'      	=> array('research', 'project_brief','persona','task_models', 'user_stories', 'style_guide'),
    	'posts_per_page'	=> -1,
    	'post__in'		=> $ids,
    	'post_status'		=> 'any',
        'orderby'        	=> 'modified',
    ));
     ?>
    <?php if($loop->have_posts()) : ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="postbox-container postbox">
    <h3><?php the_title(); ?></h3>	
    <?php if ($query->have_posts()) : ?>
    <ul>
    <?php
    	while($query->have_posts()) :
    		$query->the_post();
    ?>
    	<li><?php the_title(); ?></li>
    
    <?php endwhile; ?>
    </ul>
    <?php else: ?>
    
          <p>Oops, there are no posts.</p>
    
    <?php endif; ?>
    </div>
    <?php endwhile; endif; wp_reset_query(); ?>
  • Oh, you’ll probably want to put the query for the attached posts inside the loop and pass the ID. Maybe something like:

    
    <?php 
    $loop = new WP_Query( array( 'post_type' => 'clients', 'posts_per_page' => 10 ) );
    ?>
    <?php if($loop->have_posts()) : ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="postbox-container postbox">
    <h3><?php the_title(); ?></h3>	
    
    <?php
    $ids = get_field('attach_to_client', $loop->ID);
    
    $query = new WP_Query(array(
    	'post_type'      	=> array('research', 'project_brief','persona','task_models', 'user_stories', 'style_guide'),
    	'posts_per_page'	=> -1,
    	'post__in'				=> $ids,
    	'post_status'			=> 'any',
      'orderby'       	=> 'modified',
    ));
    ?>
    
    <?php if ($query->have_posts()) : ?>
    <ul>
    <?php
    	while($query->have_posts()) :
    		$query->the_post();
    ?>
    	<li><?php the_title(); ?></li>
    
    <?php endwhile; ?>
    </ul>
    <?php else: ?>
    
          <p>Oops, there are no posts.</p>
    
    <?php endif; ?>
    </div>
    <?php endwhile; endif; wp_reset_query(); ?>
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Trying to display posts related to other posts’ is closed to new replies.