WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Get distinct list of all posts with connections to them (6 posts)

  1. leggo-my-eggo
    Member
    Posted 2 years ago #

    I have custom post type called "artist" and a connection called "artist_to_document" as follows:

    p2p_register_connection_type( array(
      'name' => 'artist_to_document',
      'from' => 'artist',
      'to' => 'document',
      'cardinality' => 'one-to-many',
      'admin_column' => 'to'
    ) );

    What I want is a list of all artists which have "artist_to_document" connections, but I only want them once each.

    What I currently have is this:

    $connected = new WP_Query( array(
      'post_type' => 'artist',
      'connected_type' => "artist_to_document",
      'connected_items' => 'any',
      'connected_direction' => 'to',
      'nopaging' => true,
      'posts_per_page' => -1
    ) );

    Which gives me what I want, except each artist appears as many times as it has connections.

    How do I fix this so I only get each connected artist once?

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

  2. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    Well, you could define your own _groupby query var. Add this code in your functions.php file:

    function groupby_post_id( $groupby, $wp_query ) {
    	global $wpdb;
    
    	if ( 'ID' === $wp_query->get( '_groupby' ) )
    		$groupby = "{$wpdb->posts}.ID";
    
    	return $groupby;
    }
    add_filter( 'posts_groupby', 'groupby_post_id', 10, 2 );

    And then modify your query:

    $connected = new WP_Query( array(
      'post_type' => 'artist',
      'connected_type' => "artist_to_document",
      'connected_items' => 'any',
      'connected_direction' => 'to',
      'nopaging' => true,
      'posts_per_page' => -1,
      '_groupby' => 'ID'
    ) );
  3. leggo-my-eggo
    Member
    Posted 2 years ago #

    This is perfect and works beautifully, thank you. Two short followups:

    1. Just out of curiosity, why the underscore? Is that just to avoid a collision of variable names?
    2. This is returning artists which are connected to documents which are in the trash. Putting post_status in the query of course affects artists, not the documents which are connected to them. How would I exclude artists whose only connections are to documents which are in the trash?

    Thanks so much, both for your indispensable plugin and your fantastic support.

  4. leggo-my-eggo
    Member
    Posted 2 years ago #

    Bump. Any suggestions about #2 in the followup above? I can't use this while it's returning posts which are connected to posts which are in the trash.

  5. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    #2 is addressed in the development version (1.4-alpha2). You shouldn't have to do anything.

    Just out of curiosity, why the underscore? Is that just to avoid a collision of variable names?

    Yes.

  6. leggo-my-eggo
    Member
    Posted 2 years ago #

    This works perfectly now, thank you!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic