Posts 2 Posts
[resolved] How to query connected attachmets (3 posts)

  1. marlonlamancio
    Posted 3 years ago #

    Please, anyone can help me how to show attachments connected with post.

    I know that there are specific query functions in WP to achieve similar goal but I want a curated list of related images.

    I have already registered a connection type with p2p_register_connection_type hooked in wp_loaded and created some connections between posts and some attachments.

    p2p_register_connection_type( array(
    		'name' => 'attachment_to_post',
    		'from' => 'attachment',
    		'to' => 'post',
    		'title' => 'Parceiros relacionados'
    	) );

    Now how I query these connections in single post template?

    I tried using the function get_posts() and also create a new WP_Query object as shown in basic usage wiki https://github.com/scribu/wp-posts-to-posts/wiki/Basic-usage.

    I also tried using get_connected() but not returns connected attachments when I print_r() query object.

    I've read the Wiki, tried to look at the code, I've tried so many things that it is difficult to list here.

    Any help will be appreciated.


  2. activefantasies
    Posted 3 years ago #

    I'm having this problem too. The setup seems fine and I can link attachments to my CPT in WordPress Admin.

    I've just can't get the connection data back when I try to use it. I've got many other connections set up which are working fine, so I gather that it's something to do with connections to/from attachments.

    Any further info gladly appreciated - and thanks for an incredibly useful plugin.

  3. Manny Fleurmond
    Posted 3 years ago #

    $query = WP_Query( array(
        'connected_type' => 'attachment_to_post',
        'connected_items' => $post,
        'post_status' => 'inherit',
        'nopaging' => true,

    Secret is to add the post_status as inherit. I'm working on an album/gallery plugin that uses P2P as the basis and I was banging my head against the wall with this same issue.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Posts 2 Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic