WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
How To Add Info to Connection (78 posts)

  1. Derek Perkins
    Member
    Posted 3 years ago #

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

  2. scribu
    Member
    Plugin Author

    Posted 3 years ago #

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

  3. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    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');
  4. Derek Perkins
    Member
    Posted 3 years ago #

    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' ) );
    }
  5. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    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.

  6. Derek Perkins
    Member
    Posted 3 years ago #

    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
  7. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    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.

  8. Derek Perkins
    Member
    Posted 3 years ago #

    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

  9. Derek Perkins
    Member
    Posted 3 years ago #

    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

  10. Derek Perkins
    Member
    Posted 3 years ago #

    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.

  11. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    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.

  12. Derek Perkins
    Member
    Posted 3 years ago #

    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.

  13. Derek Perkins
    Member
    Posted 3 years ago #

    Is there anything keeping 0.4 from going gold?

  14. scribu
    Member
    Plugin Author

    Posted 3 years ago #

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

  15. Derek Perkins
    Member
    Posted 3 years ago #

    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>

  16. Derek Perkins
    Member
    Posted 3 years ago #

    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?

  17. ZigPress
    Member
    Posted 3 years ago #

    Hi scribu,

    I'm encountering a similar problem to Ploobers. I have two custom post types (product and shop), and I'm using P2P v0.5.1 to link them, with one item of metadata (price, a text field).

    So, I used your example from above (the comment that begins "With 0.4-alpha6, you can do this"), and initially I thought it was working fine. I made the connection reciprocal by the way.

    However, when I search and add a new connection on the product edit page, old connection data is deleted, and furthermore, the new data saved is often taken from the old data!

    Let me explain what I mean.

    On Product A, I added 2 connections at the same time, then saved it:
    Shop A (with Price 12.34)
    Shop B (with Price 56.78)

    This worked fine, 2 new records in my p2pmeta table and both connections shown in the box on the edit page.

    Then I added a third connection like this:
    Shop C (with Price set to 'free')

    On saving I ended up with ONE connection, with a new p2p_id, and ONE meta item with a new meta_id. The meta_key was 'price' (which is fine), the meta_value was 56.78 (which it has 'caught' from the earlier connection somehow).

    I've been trying to follow your code to see if there's an easy fix, but somewhere within all the inheritance and static methods I got lost!

    Advice would be much appreciated and a solution will send me to your PayPal page!

  18. ZigPress
    Member
    Posted 3 years ago #

    Never mind... in the end I wrote my own plugin to replace Posts 2 Posts - and which also lets you define connections in admin instead of using custom PHP.

    If anyone would like to try it, go here:

    http://www.zigpress.com/wordpress/plugins/zigconnect/

    It's early days so feedback (especially problems encountered) would be appreciated using the comments form of the above page.

  19. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    Always good to have more alternatives. Now I can keep Posts 2 Posts more developer oriented.

    Cheers!

  20. Derek Perkins
    Member
    Posted 3 years ago #

    scribu - Have you been able to reproduce the error that we both encountered?

  21. Modesty
    Member
    Posted 3 years ago #

    scribu - I have the same problem as Ploobers. adding new data to a 'date' field from your example deletes the previous entry... do you have any idea how to fix this?

  22. Modesty
    Member
    Posted 3 years ago #

    to clarify a bit more:

    - I've created a reciprocal connection between regular posts and a custom post type
    - if I try to connect several diferent custom post types to a single post each new metadata wipes out the previous one
    - also - if I try to add several connections to a custom post - data gets wiped out
    -however, if I try to connect different posts with one custom post, than data gets saved...

    hope this helps you to figure out what is causing the problem...

  23. ZigPress
    Member
    Posted 3 years ago #

    Modesty & Ploobers:

    You could perhaps try my ZigConnect plugin, see if it solves your problems? It's gradually maturing and is quite stable now - stable enough for me to put it in the WordPress plugin directory.

    http://wordpress.org/extend/plugins/zigconnect/

  24. Modesty
    Member
    Posted 3 years ago #

    thank you ZigPress, but from what I've seen your plug-in creates connections on a separate admin page, and for what I need, the input box must be available both on the post and custom post admin interface...

  25. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    The development version (0.7-alpha) now features a new UI:

    http://i.imgur.com/HAOwO.png

    Connections are created and deleted via AJAX requests, so whatever problems you were having with saving should be fixed now.

    To register a connection with additional metadata, just do:

    p2p_register_connection_type( array(
    	'from' => 'post',
    	'to' => 'page',
    	'fields' => array(
    		'date' => 'Date',
    		'field_key' => 'Field Title'
    	),
    	'reciprocal' => true
    ) );

    You can ditch the old code I showed you before. Setting the 'fields' arg is all that's needed.

  26. Modesty
    Member
    Posted 3 years ago #

    the only problem I have found so far with 0.7 is the fact that two posts can have only one connection.
    is it going to stay like that?

  27. Modesty
    Member
    Posted 3 years ago #

    Scribu: to clarify a bit more - you were mentioning Justin Tadlock's movie example... for this plugin to solve that problem it would be ideal if one person (custom post type) could have multiple roles i.e. connections with the same movie (custom post type)...

    with 0.7 you can store just one...

    Am I making sense?

  28. Modesty
    Member
    Posted 3 years ago #

    oh, and another question: how do we display the connection metadata on the connected post?

    to have a choice between calling the field arg & all of metadata at once would be super useful ...

  29. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    the only problem I have found so far with 0.7 is the fact that two posts can have only one connection. is it going to stay like that?

    No, I'm going to add an option to allow duplicates.

    oh, and another question: how do we display the connection metadata on the connected post?

    By using p2p_get_meta(). See here: http://scribu.net/wordpress/posts-to-posts/p2p-0-5.html

  30. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    Regarding duplication, there are two ways to achieve the multiple-role scenario:

    A) Create a duplicate connection between the same posts, with a single 'role' custom field.

    B) Create a single connection, but with multiple 'role' custom fields (the p2pmeta table supports this).

    There are advantages and difficulties in both approaches. I'll see which one makes more sense.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic