Plugin Author
scribu
(@scribu)
So you read the Looping The Loop tutorial.
Did you actually try to implement it? What does the code look like?
Thread Starter
chocks
(@chocks)
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?
Plugin Author
scribu
(@scribu)
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,
) );
Thread Starter
chocks
(@chocks)
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
Plugin Author
scribu
(@scribu)
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.