Support » Plugin: Posts 2 Posts » [Plugin: Posts 2 Posts] Displaying Connection information in the Admin Dashboard

  • Resolved dragonet35


    I have a Posts to Users connection named “checked_out.” I am working on a custom widget for my Admin Dashboard that will display the connection box information for all Posts with connections. I have mostly achieved this, but there is one issue that I can’t seem to straighten out.

    Here is what happens.
    Each post title (that has a connection) is displayed correctly, along with the connection field information – So far, so good. However, I also want it to show the Display Name of the User connected to that post. Instead of displaying that user, a list of all users with connections to any post is displayed, like so:

    Post Title1 | Copy #: 2 | Due Date: 8/1/2012
    On loan to: Username1
    On loan to: Username2
    On loan to: Username3
    On loan to: Username4

    Post Title2 | Copy #: 1 | Due Date: 7/29/2012
    On loan to: Username1
    On loan to: Username2
    On loan to: Username3
    On loan to: Username4

    My connection box is registered as:

    p2p_register_connection_type( array(
        'name' => 'checked_out',
        'from' => 'user',
        'to' => 'post',
    	'cardinality' => 'many-to-many',
    	'sortable' => 'any',
    	'admin_column' => 'any',
        'to_query_vars' => array( 'role' => 'subscriber' ),
        'title' => array( 'from' => '', 'to' => 'Checked Out To:' ),
            'fields' => array(
    	     'copy' => array(
    		'title' => 'Copy #',
    		'values' => array( '1', '2', '3', '4' )
    	      'check-out_date' => 'Check-Out Date',
    	      'due_date' => 'Due Date',

    The code within the dashboard widget is:

    $posts = get_posts(array(
         'numberposts' => -1,
         'connected_type' => 'checked_out',
         'connected_items' => $user,
    $users = get_users( array(
      'connected_type' => 'checked_out',
      'connected_items' => $post,
    	echo '<ul>';
    	foreach($posts as $post)
    	   echo '<p>';
    	   echo '<li><a href="' . get_edit_post_link($post->ID) . '">' . get_the_title($post->ID) . '</a> | Copy #: ' . p2p_get_meta( $post->p2p_id, 'copy', true ) . '</li>';
    	   echo '<li>Due Date: ' . p2p_get_meta( $post->p2p_id, 'due_date', true ) . '</li>';
    	   foreach($users as $user) {
    	      $patron = $user->display_name;
    	      echo '<li><strong>On loan to: ' . $patron . '</strong></li>';
    	   echo '</p>';
    	echo '</ul>';

    The “foreach users” loop has to be what is calling all the users with connections, but I can’t figure out how to specify the right user.

    Any suggestions would be appreciated. Thank you!

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


    Just move the get_users() call inside the foreach($posts as $post).

    Or use each_connected().

    Thanks for the suggestion! Moving get_users() into the foreach($posts as $post) almost fixes the entire issue.

    For each post that only has one connected user, it works perfectly; however, if a post has more than one connection, it still lists each user connected to the post title, instead of just that particular connection instance, like this:

    Peter Pan | Copy #1
    Date Due: 6/30/2012
    On Loan to: Wendy [only Wendy should show here]
    On Loan to: John

    Peter Pan | Copy #2
    Date Due: 8/1/2012
    On Loan to: Wendy
    On Loan to: John [only John should show for copy 2]

    Am I missing an argument in the get_users call?

    I have also been looking at each_connected(), but so far I hadn’t been able to get it to work for the dashboard, so I’ve been concentrating on the method that at least allowed me to get this far. Still learning!

    Thanks for taking the time to look at my questions.

    All right, I’ve switched things around so that the ‘copy’ and ‘due_date’ field information are being pulled from the user connection. So now I just have two identical blocks showing for an item with two connections.

    That issue is probably not plugin related, and I’ll just have to figure out if there’s a way around that or not.

    In the meantime, I have also been working on trying to get each_connected working, but that needs a new post since it’s a different question.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: Posts 2 Posts] Displaying Connection information in the Admin Dashboard’ is closed to new replies.