WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Displaying Multiple Connections (7 posts)

  1. ChrisCree
    Member
    Posted 11 months ago #

    I've got several post types with p2p connections setup like this:

    p2p_register_connection_type( array(
    	'name' => 'company_to_news',
    	'from' => 'company',
    	'to' => 'news',
    	'admin_box' => array(
    		'show' => 'to',
    		'context' => 'side',
    	),
    ) );

    When I try to display all of the various post types that are connected to a "company" in the single-company.php page template with this code I am only getting 1 post type:

    $connected_type = array( 'company_to_news', 'company_to_post', 'company_to_research', 'company_to_commentary', 'company_to_video', );
    
    $connected_company = new WP_Query( array(
    	'connected_type' => $connected_type,
    	'connected_items' => get_queried_object_id(),
    ) );

    However, when I set $connected_type = ( 'company_to_news' ); or any of the other single connections I see the corresponding posts for each of the post types as expected. So I know they are all valid connections with associated posts.

    How can I query all of the different connected post types instead of just a single type?

    The release notes on version 1.2 of the p2p plugin says,

    "Also, you can pass an array of connection types and P2P will pick the ones that make sense."

    How do we get p2p to use all the connections in the array instead of just the ones it thinks make sense?

    http://wordpress.org/plugins/posts-to-posts/

  2. scribu
    Member
    Plugin Author

    Posted 11 months ago #

    It depends on what get_queried_object_id() returns:

    If it returns a company post, then all connection types make sense, since all connections of those types have a company post at one end.

    If it returns a news post, then only company_to_news makes sense.

  3. ChrisCree
    Member
    Posted 11 months ago #

    I'm using it in the single-company.php template file. So doesn't that mean that get_queried_object_id() should return a company post?

    When I use the array of connections it just shows the built in WP post post type. It does the same thing if I try get_queried_object() too.

    What I'm trying to achieve is one list that includes all the various post types that are connected to a single company sorted by date (DESC) in the single company post template.

    Is there a way to do this with P2P? Am I trying to go about it the wrong way?

    I feel like I'm missing something painfully obvious here.

  4. scribu
    Member
    Plugin Author

    Posted 11 months ago #

    Maybe try adding 'post_type' => 'any'.

  5. ChrisCree
    Member
    Posted 11 months ago #

    That didn't do it. But adding 'post_type' => array( 'post', 'news', 'research', 'commentary', 'video' ), did!

    I assumed that adding 'post_type' to the query would conflict with the 'connected_type'.

    Thanks so much for your help, Scribu!!!

  6. scmjohns
    Member
    Posted 11 months ago #

    Chris, would you mind posting your final result? I would really appreciate seeing how this came together as I am trying to solve the exact same issue for:

    case_study, capability, client.

    On the single-client template I need to show all case_study posts for that particular client and I need to show the capability used in each case_study

    Thanks!

  7. ChrisCree
    Member
    Posted 9 months ago #

    Heya Johns! I know it's been a while. But in the off chance that you check back here, this is what I have:

    $page = get_query_var('paged');	
    
    	// Display Company News
    	$connected_type = array( 'company_to_news', 'company_to_post', 'company_to_research', 'company_to_commentary', 'company_to_video', );
    
    	$connected_company = new WP_Query( array(
            'posts_per_page' => 7,
            'nopaging' => true,
    		'paged' => $page,
            'connected_type' => $connected_type,
    		'connected_items' => get_queried_object_id(),
    		'post_type' => array( 'post', 'news', 'research', 'commentary', 'video' ),
        ) );

    Hopefully that helps you some.

Reply

You must log in to post.

About this Plugin

About this Topic