WordPress.org

Support

Support » Plugins and Hacks » [Plugin: Posts 2 Posts] How To Add Info to Connection

[Plugin: Posts 2 Posts] How To Add Info to Connection

  • I’m not sure that this is possible with a taxonomy, but I was hoping to get some feedback on the best way to store my data. My site is storing information about soccer players and teams. I have a team custom post type and a player custom post type. My team page shows all of the related players, based on Posts to Posts data previously entered.

    My problem is that in the future, when players change teams, there isn’t any way to store any kind of time related data with the existing relationship. For example, at a minimum, I’d love to be able to store some sort of tag ‘current’, so that way my team page could show the current team as well as a list of all players. Even better would be storing the years that a player was a part of the team.

    I was thinking that perhaps the taxonomy description would be a place to store that information without having to create new tables or fields. I could maybe serialize some information and put it in as part of the connection process. Does that sound like a good way to accomplish what I’m trying to do? The API might then be changed to have an optional filter based on the information stored in the description.

    While I’m writing this, I’m still brainstorming how to make it functional. I’m thinking that in P2P metabox, after you create a connection, there could be a little button added that would allow you to edit the relationship description. That wouldn’t require much of an interface than a text area, and you just assume that the developer putting data in there would also know how to read it out.

    I haven’t looked super closely at the actually hidden taxonomy, so it might not even be feasible given the current setup. Thoughts?

Viewing 15 replies - 16 through 30 (of 77 total)
  • Plugin Author scribu
    Member

    @scribu

    You rock. Do I need to run an update like previous upgrades?

    1. Download the development version again.
    2. De-activate & re-activate

    The tables should be created.

    Derek Perkins
    Member

    @ploobers

    I’m just getting a chance to look through this now. Do we not declare whether or not the connection is reciprocal anymore?

    Derek Perkins
    Member

    @ploobers

    'box' string A class that implements the P2P_Box interface. Default: P2P_Box_Multiple
    I’m glancing through the API and looking at the registration parameters. I’m assuming that this is how we are going to extend the connection box, correct? I didn’t see that code included in the alpha 4 release, so is that coming soon?

    Derek Perkins
    Member

    @ploobers

    Sorry, my bad, just found it in the ui.php.

    Derek Perkins
    Member

    @ploobers

    I’ve been working for the last couple hours on extending the metabox and I’ve got a couple of questions / ideas.
    -Removing metadata – Should metadata be automatically be deleted upon disconnect?

    Here’s a sample of the code I am adding to the p2p box. I want to add a start and end date to each connection that I am making. I copied the P2P_Box_Multiple class to my functions.php file and renamed it to a new class. Inside the loop that adds the connected item checkboxes, I added these two date fields.

    echo "Start Date:<input class='sr_datepick' id='start_date_$id' type='text' size=7>";
    echo "End Date:<input class='sr_datepick' id='end_date_$id' type='text' size=7>";

    I want to save that data, but I am looking through the save function and I can’t figure out how to get the p2p id, so I can’t see how to use the add_p2p_meta function.

    What would be the best way to tie into the update to save that metadata?

    Derek Perkins
    Member

    @ploobers

    Multiple connections to the same post:
    Here is a potential situation I might be facing. What if I want to have multiple connections to the same post? In my case, it might be a sponsored player. Maybe someone was sponsored by Nike from 2001-2004, by Adidas from 2005-2009, and in 2010 switched back to Nike again. That should be possible now with the table setup, correct?

    Plugin Author scribu
    Member

    @scribu

    Do we not declare whether or not the connection is reciprocal anymore?

    No, I didn’t like how data was duplicated. You can register a metabox with reversed parameters if you want to be able to edit connections from both sides.

    Removing metadata – Should metadata be automatically be deleted upon disconnect?

    Yes, it should, and it is.

    I want to save that data, but I am looking through the save function and I can’t figure out how to get the p2p id, so I can’t see how to use the add_p2p_meta function.

    You can use p2p_connect():

    p2p_connect($from, $to, array(
      'start_date' => $_POST["start_date_$id"],
      'start_date' => $_POST["end_date_$id"]
    ));

    Note: Should probably do some validation first.

    Multiple connections to the same post

    Yes, it is possible, as long as the metadata is different.

    Derek Perkins
    Member

    @ploobers

    No, I didn’t like how data was duplicated. You can register a metabox with reversed parameters if you want to be able to edit connections from both sides.

    So assuming that you register the reverse metabox, will it show connections that were made from the other direction?

    Multiple connections to the same post: Yes, it is possible, as long as the metadata is different.

    The UI currently doesn’t allow you to add multiple connections to the same post.

    Hooks
    I like the metabox idea, but I was also thinking about potentially adding some hooks into the default P2P_Box_Multiple. Basically, I think most users would just want to be able to hook into the loop where I did, since they will be saving data for each connection. That would keep the majority of people from having to copy a whole extra class out and would make upgrades significantly easier in the future.

    Plugin Author scribu
    Member

    @scribu

    So assuming that you register the reverse metabox, will it show connections that were made from the other direction?

    I’ll just add the ‘reciprocal’ parameter back, which will take care of the whole thing.

    I like the metabox idea, but I was also thinking about potentially adding some hooks into the default P2P_Box_Multiple.

    I think it’s too early for that, but I’m not against the idea.

    Derek Perkins
    Member

    @ploobers

    I’m running into some problems with my metabox. I’m trying to save my metadata. I replaced your p2p_connect with this loop.

    foreach ( array_diff( $new_connections, $old_connections ) as $nc ) {
    	$start_date = $_POST[ 'start_date_' . $nc ];
    	$end_date = $_POST[ 'end_date_' . $nc ];
    	$data = array( "start_date" => $start_date, "end_date" => $end_date );
    	p2p_connect( $post_a, $nc, $data );
    }

    If I run the code above, the metadata can only be inserted when the connection is initially made, because it won’t run the connect code afterwards.

    foreach ( $new_connections as $nc ) {
    	$start_date = $_POST[ 'start_date_' . $nc ];
    	$end_date = $_POST[ 'end_date_' . $nc ];
    	$data = array( "start_date" => $start_date, "end_date" => $end_date );
    	p2p_connect( $post_a, $nc, $data );
    }

    If I run this loop, it connects the post again every time the post is updated. My metadata is inserted correctly, but with a new p2p_id each time the update is called.

    Once I disconnect the posts, it deletes ALL of the metadata. If there are multiple connections, it should only delete the metadata for that specific connection.

    Maybe I’m missing something in the API, but it seems to me that the p2p_id field needs to be added to the metabox that I can use as a variable. That will solve two problems.
    1) That will be necessary in order for the UI to be able to have multiple connections to the same post.
    2) I can update the post meta directly, so that it can be changed after the connection is originally made.

    Hook Implementation
    I think hooks would be the best way to deal with this, by hiding core functionality and making P2P much more accessible to the average developer. I think that you could get by with making three hooks available.
    1) box: Insert an action hook inside the existing loop per displayed connection. You can insert form fields here or a link to an AJAX form or something if you have more intense connection details.
    2) save: Have a save loop similar to what I have proposed here, with a filter hook that lets you edit the $data parameter based on a p2p_id variable.
    3) load: I haven’t look much at this, but you’d need a similar hook to the save function in here to let you display your data.

    Derek Perkins
    Member

    @ploobers

    I’ll just add the ‘reciprocal’ parameter back, which will take care of the whole thing.

    Even with reciprocal data, you shouldn’t have to duplicate the information again. You could just add an extra boolean field to the table that is ‘reciprocal’, then alter the get connected SQL query to look at both fields.

    Plugin Author scribu
    Member

    @scribu

    development version (0.4-alpha5):

    The ‘reciprocal’ arg is back. Instead of adding an extra field to the table, I added a $reversed flag to the P2P_Box_Multiple class.

    You can use the $this->box_id variable to differentiate between boxes.

    Once I disconnect the posts, it deletes ALL of the metadata. If there are multiple connections, it should only delete the metadata for that specific connection.

    You have to pass $data to p2p_disconnect() too.

    Maybe I’m missing something in the API, but it seems to me that the p2p_id field needs to be added to the metabox that I can use as a variable.

    p2p_get_connected() now returns array(p2p_id => post_id).

    PS: I think you’ll find the changelog useful.

    Derek Perkins
    Member

    @ploobers

    I’m working on this again, but my feeling as I’m diving through the code again is that this is far too technical for typical usage. To try and figure out what kind of data I’m supposed to pass into the $data argument, I’m having to follow your function changes through the boxes.php, api.php and storage.php files, and I’m still not quite sure what to pass and what the best method is for storing connection specific data.

    Derek Perkins
    Member

    @ploobers

    Could you provide an example for how to store a single piece of metadata per connection?

    Derek Perkins
    Member

    @ploobers

    scribu – Any hints as the best way to implement a connection with attached meta data?

Viewing 15 replies - 16 through 30 (of 77 total)
  • The topic ‘[Plugin: Posts 2 Posts] How To Add Info to Connection’ is closed to new replies.