WordPress.org

Forums

Posts 2 Posts
Returning unrelated posts when connected to a draft post. (5 posts)

  1. isaacalves
    Member
    Posted 1 year ago #

    get_related() (or get_connected() ) is returning unrelated posts when the related post is a draft.

    Example:

    I have the post "A1" , which is connected to the post "B4", where the letter refers to the post type and the number to the post ID.

    If post "B4" is published, get_related() on "A1" will return 'A' posts that are linked to post "B4". Great.

    The problem is, as I set post "B4" status to 'draft', get_related() on "A1" will return a lot of "A" posts unrelated to "B4".

    Why? I expect it to return anything. Maybe that's a default implementation that will return some random posts case there are no related posts? It is returning always the same ten posts (out of a lot more) though.

    Hope that's clear. Thanks in advance.

  2. isaacalves
    Member
    Posted 1 year ago #

    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.

  3. isaacalves
    Member
    Posted 1 year ago #

    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) : ?>

  4. isaacalves
    Member
    Posted 1 year ago #

    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

  5. isaacalves
    Member
    Posted 1 year ago #

    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.

  6. isaacalves
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags