Don’t Worry, You’re In The Right Place
A lot of these posts were originally on SureFireWebServices.com. So if you found them through search engines or clicking around, this is their new home :). Enjoy!

Using Posts 2 Posts with Views

This post is picking up from my last one Many to Many Posts Made Easy so if you haven’t read that, I suggest you start there.

In the last post, I went over how to create the relationships and display them via the standard WordPress loop. In this post, I’ll briefly go over how to replace the standard loop, and just use Views.
[themedy_alertbox icon=”exclamation-circle” colour=”blue” font_awesome_att=”” custom_colour=””]Please Note: I have not had a chance to test this yet and I wanted to do a video. Once I get some free time, I’ll make the video and test it all in one.[/themedy_alertbox]

I found this information hidden deeply within the WP-Types.com support forum. If you’ve ever been there, you’ll know how hard it is to find info, but this was something I just had to save.

Let’s Get To It

The goal is to get the related posts to display.  The trick is to get Views to actually relate the posts since P2P isn’t a Views addon or anything.  So in the most basic way, what we would need is the ID’s of the posts that are related.  Something like:

[ wpv-post-field name="custom-field" id="ids of the related posts"]

In order to get this, we’re going to have to create a new shortcode that will get the ID’s then set a filter.

The Awesome Sauce

Here’s the shortcode you need:


add_shortcode('wpv-post-p2p-id', 'p2p_connected_id_shortcode');
function p2p_connected_id_shortcode($atts) {
    global $post;
    $connected = get_posts(array(
        'connected_type' => $atts['type'],
        'connected_items' => $post->ID
    ) );
    if (!empty($connected)) {
        return reset($connected)->ID;
    }
}

Add this to your functions.php file.  Caridad from the Toolset Team created that shortcode and it’s really so awesome in it’s simplicity and function.  Now check out how this awesome shortcode is used.

When you’re in you View, you would do something like:

Post ID: [ wpv-post-p2p-id type="posts_to_posts" ]

Don’t include the spaces

‘Posts_to_posts’ is just an example, you would use the name for the relationship you created. The snippet above will display all the ID’s of the related posts.

Title of Posts:  [ wpv-post-title id="[ wpv-post-p2p-id type="posts_to_posts" ]" ]

Don’t include the spaces

The snippet above will return all the titles of the related posts.

Now the last step is to just add the filter.

filter

And you’re done!!

Now you have access to all the ID’s of the related posts and you can display them however you like with views!!!

Advertisement
About the Author
What's UP! This is my site, I write 99% of the articles on here. I'm also the owner of SureFireWebServices.com. I help out a lot of developers and designers getting into the web game. Helping is fun for me, so feel free to ask me any questions! I've made courses and have a membership as well to help get you on your feet!
Scroll to top