WordPress.org

Support

Support » Plugins and Hacks » Posts 2 Posts » [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 - 31 through 45 (of 77 total)
  • Plugin Author scribu

    @scribu

    I plan to write an example implementation but it will take a few days as I’m busy IRL.

    Plugin Author scribu

    @scribu

    With 0.4-alpha6, you can do this:

    function my_p2p_init() {
    
    	class My_P2P_Box_Multiple extends P2P_Box_Multiple {
    
    		protected $meta_keys = array( 'date' );
    
    		function connection_template( $post_id = 0, $p2p_id = 0 ) {
    			if ( $post_id ) {
    				$post_title = get_the_title( $post_id );
    				$date = p2p_get_meta( $p2p_id, 'date', true );
    			} else {
    				$post_id = '%post_id%';
    				$post_title = '%post_title%';
    				$date = '';
    			}
    
    ?>
    			<li>
    				<label>
    					<input type="checkbox" checked="checked" name="<?php echo $this->input_name( array( 'post_id', '' ) ); ?>" value="<?php echo $post_id; ?>">
    					<?php echo $post_title; ?>
    				</label>
    
    				<label>
    					Date:
    					<input type="text" name="<?php echo $this->input_name( array( 'date', '' ) ); ?>" value="<?php echo esc_attr( $date ); ?>">
    				</label>
    			</li>
    <?php
    		}
    	}
    
    	p2p_register_connection_type( array(
    		'from' => 'post',
    		'to' => 'recipe',
    		'box' => 'My_P2P_Box_Multiple',
    	) );
    }
    
    add_action('init', 'my_p2p_init');

    scribu – Thanks for the implementation help. When I had it set up just how you had it pasted here, it worked for me. I tweaked it just slightly and added one more field that ended up breaking the implementation. Checking the database, this code is actually storing the data, but with a new p2p_id each time, and each time I update, the old values are all deleted. What did I change that messed it up?

    function my_p2p_init() {
    
    	class My_P2P_Box_Multiple extends P2P_Box_Multiple {
    
    		protected $meta_keys = array( 'startdate', 'enddate' );
    
    		function connection_template( $post_id = 0, $p2p_id = 0 ) {
    			if ( $post_id ) {
    				$post_title = get_the_title( $post_id );
    				$date = p2p_get_meta( $p2p_id, 'date', true );
    			} else {
    				$post_id = '%post_id%';
    				$post_title = '%post_title%';
    				$date = '';
    			}
    
    ?>
    			<li>
    				<label>
    					<input type="checkbox" checked="checked" name="<?php echo $this->input_name( array( 'post_id', '' ) ); ?>" value="<?php echo $post_id; ?>">
    					<?php echo $post_title; ?>
    				</label>
    
    				<label>
    					Start Date:
    					<input type="text" class="datepicker" size=7 name="<?php echo $this->input_name( array( 'startdate', '' ) ); ?>" value="<?php echo esc_attr( $date ); ?>">
    				</label>
                    <label>
    					End Date:
    					<input type="text" class="datepicker" size=7 name="<?php echo $this->input_name( array( 'enddate', '' ) ); ?>" value="<?php echo esc_attr( $date ); ?>">
    				</label>
    
    			</li>
                <script type="text/javascript">jQuery(function() { jQuery('.datepicker').datepicker({ changeMonth: true, changeYear: true }); }); </script>
    
    <?php
    		}
    	}
    
    	p2p_register_connection_type( array( 'from' => 'player', 'to' => 'shoe', 'title' => 'Player to Shoe', 'reciprocal' => true, 'box' => 'My_P2P_Box_Multiple' ) );
    }
    Plugin Author scribu

    @scribu

    Two things:

    Instead of:

    $date = p2p_get_meta( $p2p_id, 'date', true );

    you should have:

    foreach( $this->meta_keys as $key )
      $$key = p2p_get_meta( $p2p_id, $key, true );

    Then, instead of:

    echo esc_attr( $date );

    you should have:

    echo esc_attr( $startdate ); and

    echo esc_attr( $enddate ); respectively.

    That’s embarrassing that I missed those, I guess it was later than I thought. I’ve got it fixed now and working like a charm. The reciprocal flag is working as expected and I don’t foresee any further issues.

    I really like the connection template you set up. I think that will make it infinitely easier to customize than doing the entire meta box. Here are my remaining thoughts before finalizing the 0.4 release.

    • Connection import – I’ve got thousands of connections from the previous taxonomy version that haven’t been brought in yet
    • Multiple connections – This isn’t super urgent for me, but is definitely something that I will need sometime soon. The data schema should already support this, so it should just be a question of hooking up the UI
    Plugin Author scribu

    @scribu

    Just landed the migration procedure in the development version (0.4-beta).

    Same procedure as in 0.3:

    If you were using an older version of the plugin, go to /wp-admin/?migrate_p2p to migrate your connections. You should probably make a database backup beforehand, just in case.

    I haven’t done a full analysis yet, but I just did my import, but I don’t think it imported successfully.

    Pre-import
    # of p2p taxonomy terms: 3,216
    # of rows in p2p table: 1

    Post-import
    # of p2p taxonomy terms: 858
    # of rows in p2p table: 5,012

    I don’t know why the numbers aren’t matching up, but here are some ideas.
    – All of my previous connections were reciprocal, which may cause doubling in the import.
    – The import script may have timed out

    I tried a few other times and it definitely timed out. I continued running the script until it stopped timing out, and here were my results.

    Post-import (multiple times)
    # of p2p taxonomy terms: 8
    # of rows in p2p table: 6,692

    I also ran the import again after increasing the max php script execution time and got the same results as running the script multiple times, so it doesn’t appear that the script being cut off causes any problems.

    Plugin Author scribu

    @scribu

    No, timeouts are not a problem, since each term is deleted only after all the connections for it are migrated.

    All of my previous connections were reciprocal, which may cause doubling in the import.

    Indeed; I din’t account for this.

    Also, you shouldn’t be looking at the number of terms, but at the number of rows in the wp_term_relationships table.

    Ok. I restored my previous database again and the p2p term_relationships is the same as the number of rows being imported into the new p2p table.

    I guess I’ll restore my database again and wait to run the import until the plugin takes care of reciprocal data.

    Is there anything keeping 0.4 from going gold?

    Plugin Author scribu

    @scribu

    Yes: connections aren’t reliably removed, but I have an idea on how to fix it.

    I’ve got a question on how to activate my datepicker. I use the following code which works when I load a page that already has these fields available. It doesn’t work when I first add a new connection and the extra fields are shown, since they weren’t available when the page loaded. How would you suggest having it apply to the newly created fields?

    <script type="text/javascript">jQuery('.datepicker').datepicker({ changeMonth: true, changeYear: true }); </script>

    Using that P2P_Box_Multiple extension code from above, my data isn’t storing correctly. Every time that I add new connections, it deletes all of the old ones. How do I fix it so that it stores both?

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