Support » Plugin: Posts 2 Posts » Find posts with no user connection

  • Resolved TCBarrett


    Loving your plugin Scribu.

    I need to create a drop down of posts that have not been connected with any users. How do I add that to my get_posts() arguments?

    Noddy (expensive) example:

    function get_unconnected_suppliers(){
      $available = array();
        $suppliers = get_posts( array(
          'post_type'      => 'supplier',
          'post_status'    => 'publish',
          'numberposts'    => -1,
          'posts_per_page' => -1,
          'orderby'        => 'title',
          'order'          => 'ASC',
        ) );
        foreach( $suppliers as $supplier ) :
          $users = get_users( array(
            'connected_type'  => 'user_supplier',
            'connected_items' => $supplier
          ) );
          if( $users ):
        $available[] = $supplier;
      return $available;


Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author scribu


    You could use

    $posts = p2p_type( 'YOUR_CONNECTION_TYPE' )
      ->set_direction( 'from' )
      ->get_connectable( 'any' );

    Replace ‘from’ with ‘to’, if necessary.

    Thanks. Is that for the complete list of posts, or to be called inside the loop to check for connections?

    Plugin Author scribu



    Gotacha, like so?

    $suppliers = p2p_type( 'user_supplier' )->set_direction( 'to' )->get_connectable( 'any' );
    foreach( $suppliers->items as $supplier ):
      $available[] = $supplier;

    This is not any quicker. My sample size is ~1500 posts and it is just as slow as my first example.

    Perhaps I should identify all existing connections instead?

    Plugin Author scribu


    Well, get_connetable() actually uses get_connected() internally, so I’m not sure if you’ll gain much.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Find posts with no user connection’ is closed to new replies.