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>
There are two critical things you need to do when setting up your query.
You need to set all the post types you want to look for and you need to set all the connection types you want to look for. Setting up multiple values for post type or connection type is done with arrays.
Quick example below. Of course you need to set all the other arguments for your query, but this should contain all the key arguments you need. Don’t just copy the code since your connection type names are probably a bit different than in this quick example of mine.
—edit—
Answer to your follow-up question below. You could mix your two separate arrays like this. Just create array of your posts and array of your agendas. Mix and output the mixed array. NOT SURE if this is the smartest way. Not really sure what you actually want to achieve. But this would work no matter how many posts or agendas.