WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Find posts with no user connection (6 posts)

  1. TCBarrett
    Member
    Posted 1 year ago #

    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 ):
            continue;
          endif;
    
        $available[] = $supplier;
      endforeach;
    
      return $available;
    }

    http:///extend/plugins/posts-to-posts/

  2. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    You could use

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

    Replace 'from' with 'to', if necessary.

  3. TCBarrett
    Member
    Posted 1 year ago #

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

  4. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    ALL THE POSTS! :)

  5. TCBarrett
    Member
    Posted 1 year ago #

    Gotacha, like so?

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

    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?

  6. scribu
    Member
    Plugin Author

    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.