Let's say I've got 3 post types: Posts, agenda and charity. I use the plugin Posts2posts from scribu to connect certain posts and certain agenda items to some charity items.
On the single page of charity I want to display posts and agenda in the sidebar. I have two problems:
1st problem: I want them mixed, but still sorted on date
2nd problem: I want to display only the posts and agenda items connected to my charity post.
It will look something like
-Last post item connected to this charity
-Last agenda item connected to this charity
-Forlast post item connected to this charity
-Forlast agenda item connected to this charity
When using wp_query, I have no idea how to mix those post types and let them display in the right order, only using connected ones. When using scribu's example, I still have no idea how to mix them... :(
Does someone has an idea of how I can do this?
UPDATE
I got the query working with jleander's answer.
I used 'orderby' => 'date'
and 'order' => 'DESC'
to display the latest posted item first.
But I want to display them alternatly, like:
last post
last agenda item
forlast post
forlast agenda item
Code after great help of jlaender:
<div class="aside halfwidth">
<?php
$post_types = array('post','agenda');
$connection_types = array('post_to_doel','agenda_to_doel');
$args = array(
'connected_type' => $connection_types,
'connected_items' => get_queried_object_id(),
'post_type' => $post_types,
'order' => 'DESC',
'orderby' => 'date'
);
$connected_stuff = new WP_Query($args);
if ( $connected_stuff->have_posts() ) :
?>
<h3>Related pages:</h3>
<?php
$posts = array();
$agendas = array();
$results = array();
$posts[0] = 'first post';
$posts[1] = 'second post';
$posts[2] = 'third post';
$agendas[0] = 'first agenda';
$agendas[1] = 'second agenda';
$agendas[2] = 'third agenda';
while(!empty($posts) || !empty($agendas)) {
if(!empty($posts)) {
$results[] = array_shift($posts);
}
if(!empty($agendas)) {
$results[] = array_shift($agendas);
}
}
foreach($results as $result){
echo $result; ?>
<div class="connecteditems">
<!--LOOPED HTML-->
</div>
<?php } ?>
<?php
// Prevent weirdness
wp_reset_postdata();
endif;
?>
</div>