I solved it adding the following code after the get_related() query:
$wp_related = p2p_type( 'portfolio' )->get_related( get_queried_object() );
// check if connected post is a draft:
$connected = get_posts( array(
'connected_type' => 'portfolio',
'connected_items' => get_queried_object_id()
) );
if (!$connected)
$wp_related = null;
I won’t mark this topic as resolved as this is just a workaround for me. I’d still like to understand why I get all those unrelated posts.
Turns out that setting $wp_related to null slightly messed up things so I did this:
$client_is_published = $connected ? 1 : 0;
and used later while checking for posts:
<?php if ( $wp_related->have_posts() && $client_is_published) : ?>
So, that turnaround unfortunately haven’t solved my problem, because It’ll return me nothing when the related post is a draft. It should return me all ‘A’ posts related to the draft ‘B’ post (the letter refers to the custom post type, as I mentioned above)
I need a list of posts connected to a draft-post connected to a post – is that possible?
Any help will be greatly appreciated!
Cheers
BTW, I just landed on this job where I have to deal with this. I didn’t implement the posts-to-posts plugin myself, neither have I any experience with this plug-in (great plug-in though). Guess it’s worth saying.
I believe some people might have run through this problem, so I’ll try to be more concise:
I have two custom post types linked to each other: PROJECT and CLIENT.
In a single-PROJECT page, I get all other PROJECTS related to the CLIENT related to that PROJECT.
The CLIENTS template shows a list of all clients.
However, It shouldn’t show all CLIENTS there, so I set some CLIENTS as draft.
Hence the problem: in a single-PROJECT page related to a CLIENT which had been set as ‘draft’, I’ll get a bunch of projects unrelated to that client.
I guess I’ll have to create a custom metabox on the CLIENT post type where I’ll be able to set whether it should show up on the CLIENTS page or not, and un-mark them as draft.