1

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>

1 Answer 1

1

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.

$post_types = array('post','agenda');
$connection_types = array('post_to_charity','agenda_to_charity');
$args = array(
    'connected_type' => $connection_types,
    'connected_items' => get_queried_object_id(),
    'post_type' => $post_types
    );
$connected_stuff = new WP_Query($args);

---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.

$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;
}
9
  • Thank you, I got it to work :) The only problem I'm Still struggling with is that I want themn to display alternately(I don't know if this is the good word, but like the even selector from css) So post, agenda, post, agenda Do you know if it is possible?
    – Maartje
    Commented Jan 16, 2014 at 13:12
  • So you want every other to be agenda and every other to be post? This kind of makes no sense if you at the same time want them to be ordered by release date/time. It will probably just be very confusing for your users. Anyway you could probably achieve this quite easily by making two separate queries for posts and agendas and arranging them by pushing to an array in your desired order. Commented Jan 21, 2014 at 10:49
  • There are no other posts, just 4, pt1, pt2, pt1, pt2. But the design will make clear what is agenda and what is post :) thanks for your suggestion, but I have no idea how to achieve this, I think I will go take a look at UNION for merging query's, can be found over here: wordpress.stackexchange.com/questions/112582/… The only problem with this is that I have to find out how to connect this with my posts2posts loop
    – Maartje
    Commented Jan 21, 2014 at 10:49
  • Check my edit above. Commented Jan 21, 2014 at 11:05
  • Thank you! I placed the html that should be looped inside the forech loop, but I think I am doing something wrong, because it shows me 6 times the charity item I'm on right now. So I think it don't reconize post type.. See the above edit of my post for my current code
    – Maartje
    Commented Jan 21, 2014 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.