WordPress.org

Forums

Posts 2 Posts
[resolved] Looping the loop.. reversed? (6 posts)

  1. chocks
    Member
    Posted 3 years ago #

    I love this plugin Scribu (yesterday I found the need to buy you a beer and sent a donation through your site!)

    It'd be awesome if you could help me out cracking this bit of functionality: I have two post types: offices and jobs. I want to display the offices that have jobs and list them accordingly:

    <li class="drop-list"><span>Office Name 1</span>
                  <ul class="accord-list">
                    <li><a href="link">Job Name 1</a></li>
                    <li><a href="link">Job Name 2</a></li>
                  </ul>
                </li>

    The registration connection is:

    p2p_register_connection_type(
    		array(
    		'name' => 'jobs_to_offices',
    		'from' => 'jobs',
    		'to' => 'offices',
    		'reciprocal' => true,
    		'cardinality' => 'many-to-many',
    		'title' => 'Related Jobs',
    		)

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    So you read the Looping The Loop tutorial.

    Did you actually try to implement it? What does the code look like?

  3. chocks
    Member
    Posted 3 years ago #

    Thanks for dropping by Scribu! I wrote the following:

    <?php
    	$connected_offices = p2p_type( 'jobs_to_offices' )->get_connected();
    	$show_offices = new WP_Query("post_type=offices&posts_per_page=-1&post__in=$connected_offices");
    	$i=1; while ($show_offices->have_posts()) : $show_offices->the_post();
    ?>
    	<li class="drop-list"><span><?php the_title();?></span>
    	        <ul class="accord-list">
    			<?php
    			$connected = p2p_type( 'jobs_to_offices' )->get_connected( $post );
    			while ( $connected->have_posts() ) : $connected->the_post();
    			?>
    				<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
    			<?php
    			// Prevent weirdness
    			endwhile;
    			wp_reset_postdata();
    			?>
    		</ul>
    	</li>
    <?php endwhile;	?>

    But it brought me all the offices instead of the ones that actually have active connections. I thought that by sending $connected_offices = p2p_type( 'jobs_to_offices' )->get_connected(); to post__in it would do the trick.. but it didn't..

    Any clue?

  4. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    Either use get_connected() - which alredy returns a WP_Query instance - or create a WP_Query instance manually; not both.

    Since you want to get all the offices, you have to set the direction explicitly:

    $show_offices = new WP_Query( array(
      'post_type' => 'offices',
      'connected_type' => 'jobs_to_offices',
      'connected_items' => 'any',
      'connected_direction' => 'to',
      'nopaging' => true,
    ) );
  5. chocks
    Member
    Posted 3 years ago #

    Thanks for this scribu!
    I ended up doing this:

    <?php
    	$offices_shown=array();
           	$connected_offices = p2p_type( 'jobs_to_offices' )->get_connected();
    	$show_offices = new WP_Query( array(
    		  'post_type' => 'offices',
    		  'nopaging' => true,
    		  'connected_type' => 'jobs_to_offices',
    		  'connected_items' => 'any',
    		  'connected_direction' => 'from'
    	) );
    	$i=1; while ($show_offices->have_posts()) : $show_offices->the_post();
    	$office_id = $post->ID;
    	if (!isset($offices_shown[$office_id])) :
    	$offices_shown[$office_id]=1;
    ?>
    	<li class="drop-list"><span><?php the_title();?></span>
                  <ul class="accord-list">
                 	<?php
    		$connected = p2p_type( 'jobs_to_offices' )->get_connected( $post );
    		while ( $connected->have_posts() ) : $connected->the_post();
    		?>
    			<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
    		<?php
    		// Prevent weirdness
    		endwhile;
    		wp_reset_postdata();
    		?>
    	     </ul>
    	</li>
    	<?php endif; endwhile;	?>

    I had to change the direction you set to "from" as the other way around it would give me a list of jobs with the connected offices and I wanted to list the offices with the connected jobs. Then I had to set an array to hide the offices displayed as it would repeat for some reason an office with jobs. In the end I got what I was looking for although the 'array hack' isn't my favorite approach!
    Thanks a lot,

    J

  6. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    Glad to hear you figure it out. Note that you can remove this line:

    $connected_offices = p2p_type( 'jobs_to_offices' )->get_connected();

    since you don't use $connected_offices anywhere.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Posts 2 Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic