• Hi scribu,

    First of all; a BIG thank you for this brilliant plugin! This saved me so much work. As a matter of fact I think this should be a standard function in WordPress 4 or whatever ??

    I was wundering how I can access the values in a connection field from a connection type? For example, I have the following connection type with the connection field called childtype:

    $participant_connection = p2p_register_connection_type( array(
            'from' => 'post-type-parent',
    		'to' => 'post-type-child',
    		'fields' => array(
    			'childtype' => array(
    				'title' => 'Type',
    				'values' => array('value1', 'value2', 'value3', 'value4','value5')
    			)
    		)
        ) );

    Which works great by the way, I get the childtype within the loop like this:
    p2p_get_meta($post->p2p_id, 'childtype', true);

    But I want to get all the values from the childtype->values array outside the loop and use them in a foreach loop. Something like this:

    $terms = p2p_get_meta($connected->childtype->values);
    foreach($terms as $type):
      echo $type->value;
    endforeach;

    Obviously $terms = p2p_get_meta(childtype->values); isn’t working yet, but that’s the idea. How should I access these values?
    Thanks for your time so far!

    Cheers, Toine

Viewing 15 replies - 1 through 15 (of 16 total)
  • So, you want to get back the array('value1', 'value2', 'value3', 'value4','value5') you put in?

    Or all the ‘childtype’ values that have at least one connection?

    Or all the connections with a particular value?

    Or what?

    Thread Starter Twansparant

    (@twansparant)

    Hi scribu, thanks for your reply.
    Yes I want to get back all the ‘childtype’ values that have at least one connection (I’m using it in combination with the domtab javascript). How do I achieve that exactly?
    Thanks!

    I’m afraid you’re going to have to do a direct SQL query for that.

    See https://github.com/scribu/wp-posts-to-posts/wiki/Data-organization

    Thread Starter Twansparant

    (@twansparant)

    And what about just getting the values regardless if it has a connected post or not?

    Actually, if you don’t need the post ids, the query is pretty simple:

    $values = $wpdb->get_col( "SELECT DISTINCT meta_value FROM $wpdb->p2pmeta WHERE meta_key = 'childtype'" );
    Thread Starter Twansparant

    (@twansparant)

    Hi thanks for that,

    Can’t seem to get this working, echo $terms does give me an array, but I’m not sure how I should do the foreach loop? I’m not really an expert on SQL queries I have to admit…

    $terms = $wpdb->get_col("SELECT DISTINCT meta_value FROM $wpdb->p2pmeta WHERE meta_key = 'childtype'" );
    
    foreach($terms as $type):
    	$connected = $participant_connection->get_connected( get_queried_object_id(), array(
    		'connected_meta' => array('childtype' => $type )
    	) );
    	if ($connected->have_posts() ) :
    		echo '<li><a href="#'.$type.'">'.$type.'</a></li>';
    	endif;
    endforeach;

    I want to make an list item for every value (value1, value2 etc).
    Any ideas on this?
    Thanks!

    With the development version (0.9.5-alpha), you can do this:

    $connected = $participant_connection->get_connected( get_queried_object_id() );
    
    $buckets = p2p_post_buckets( $connected, 'childtype' );
    
    foreach ( $buckets as $type => $bucket ) {
      echo '<li><a href="#'.$type.'">'.$type.'</a></li>';
    }

    and later in the file:

    foreach ( $buckets as $type => $bucket ) {
      p2p_list_posts( $bucket );
    }

    Actually, instead of p2p_post_buckets() use p2p_split_posts(). I figured that’s a better name for it.

    Thread Starter Twansparant

    (@twansparant)

    Hi scribu,

    Thanks, I tested it and it workes great! However I still have a few questions:

    1) Is it possible to sort the output of the p2p_split_posts() array, the same way as the order of the entries in 'values' => array('value1', 'value2', 'value3', 'value4','value5') ?

    Right now, the only way to sort the array is by setting 'sortable' => '_order', and re-arrange the connected posts in the order you want.
    You can use asort($buckets, SORT_STRING); offcourse, but I want it to have a particular order, or am I asking too much now?

    2) How can I use my own p2p_list_posts( $bucket ); function if I don’t want a list of the posts, but my own structure?
    p2p_list_posts( $bucket ); works great by the way, I get a list of the connected posts from that childtype. But if I start a new loop within the foreach like this:

    <?php if ( $connected->have_posts() ) :
    while ( $connected->have_posts() ) : $connected->the_post(); ?>
    <h3><a href="<?php echo get_post_meta($post->ID, 'participanturl', true); ?>" title="Open website" target="_blank"><?php the_title(); ?></a></h3>
    <?php endwhile;
    endif; ?>

    I get all the connected posts, not sorted by childtype. How does this work exactly?

    3) If I use a development version of your plugin, is it safe to update your plugin in the future without losing essential data?

    Thanks for your time again!
    Cheers, Toine

    Thread Starter Twansparant

    (@twansparant)

    Oh by the way, I’m getting an error with the development version (0.9.5-alpha) when deleting an image from my media library:

    Fatal error: Call to undefined method P2P_Storage::get() in /mydomain/wp-content/plugins/posts-to-posts/core/storage.php on line 80

    Just to let you know

    Thread Starter Twansparant

    (@twansparant)

    I’ve got number 2 figured out!
    This works perfectly with domtab:

    <?php foreach ( $buckets as $type => $bucket ) { ?>
    	<div class="tab">
    		<a name="<?php echo $type; ?>" id="<?php echo $type; ?>"></a>
    		<?php
    		foreach ( $bucket as $post ) { ?>
    			<div class="post-block">
    				<?php the_post_thumbnail('thumbnail');?>
    				<h3><a href="<?php echo get_post_meta($post->ID, 'participanturl', true); ?>" title="Open website" target="_blank"><?php the_title(); ?></a></h3>											<?php echo p2p_get_meta($post->p2p_id, 'project', true); ?>
    			</div>
    		<?php } ?>
    	</div>
    <?php } ?>

    For question 1 I still don’t have a solution though…

    1) Is it possible to sort the output of the p2p_split_posts() array, the same way as the order of the entries in ‘values’ => array(‘value1’, ‘value2’, ‘value3’, ‘value4′,’value5’) ?

    Like this:

    $GLOBALS['my_childtypes'] = array('value1', 'value2', 'value3', 'value4','value5');
    
    p2p_register_connection_type( array(
    	...
    	'fields' => array(
    		'childtype' => array(
    			'title' => 'Type',
    			'values' => $GLOBALS['my_childtypes']
    		)
    	)
    );

    and later:

    ...
    
    foreach ( $GLOBALS['my_childtypes'] as $type ) {
    	if ( !isset( $buckets[$type] ) )
    		continue;
    
    	$bucket = $buckets[$type];
    	...
    }

    3) If I use a development version of your plugin, is it safe to update your plugin in the future without losing essential data?

    Yes.

    Fatal error: Call to undefined method P2P_Storage::get() in /mydomain/wp-content/plugins/posts-to-posts/core/storage.php on line 80

    Should go away if you re-download now.

    Thread Starter Twansparant

    (@twansparant)

    Brilliant ??
    Thanks for all your help, appreciate it!

    Thread Starter Twansparant

    (@twansparant)

    Hi scribu,

    After upgrading to the 1.1 plugin version, the p2p_split_posts doesn’t work anymore.

    I addressed the issue over here and I put the function in my functions.php like you suggested.

    The error Call to undefined function p2p_split_posts() is gone obviously, but the connected posts are not showing up anymore in my domtabs.

    I use this code to get the connected posts and split the post types into domtabs:

    $connected = p2p_type('event-participant')->get_connected( get_queried_object_id(), array(
    	'suppress_filters' => true, // necessary in combination with the sticky-custom-post-types plugin
    	'posts_per_page' => -1,
        'orderby' => 'title',
    	'order' => 'ASC'
    ) );
    $buckets = p2p_split_posts( $connected, 'participanttype' );
    foreach ( $GLOBALS['participant_types'] as $type ) {
    	if ( !isset( $buckets[$type] ) )
    		continue;
    	$bucket = $buckets[$type];
    	echo '<li><a href="#'.$type.'">'.$type.'</a></li>';
    } ?>

    To populate the domtab div’s with the corresponding posts I’m using this code:

    foreach ( $buckets as $type => $bucket ) { ?>
    	<div class="tab">
    		<a name="<?php echo $type; ?>" id="<?php echo $type; ?>"></a>
    		<?php foreach ( $bucket as $post ) {
    		// Posts } ?>
    	</div>
    <?php } ?>

    I setup my connection type like this:

    $GLOBALS['participant_types'] = array('value1', 'value2', 'value3', 'value4','value5');
        p2p_register_connection_type( array(
        	'id' => 'event-participant',
            'from' => 'fiber-event',
    		'to' => 'participant',
    		'sortable' => '_order',
    		'reciprocal' => true,
    		'prevent_duplicates' => false,
    		'title' => 'Event - Participant connection',
    		'fields' => array(
    			'project' => 'Project',
    			'featuredimage' => 'Attachement ID',
    			'participanttype' => array(
    				'title' => 'Type',
    				'values' => $GLOBALS['participant_types']
    			)
    		),
    		'admin_box' => array('show' => 'any', 'context' => 'advanced')
        ) );

    Any idea why it’s not working anymore?
    When I downgrade to the 1.0.1 version, it works again?

    Thanks!

    I have no idea why it works at all, given this line:

    'suppress_filters' => true, // necessary in combination with the sticky-custom-post-types plugin

    Try removing it. I have a hunch that in 1.0.1 it’s simply overwritten.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘[Plugin: Posts 2 Posts] Access values in connection field array?’ is closed to new replies.