-
Notifications
You must be signed in to change notification settings - Fork 261
Connection metadata
With Posts 2 Posts, it's possible to store arbitrary information per connection, i.e. to have connection metadata.
The fields a user can edit are defined via the 'fields' argument:
<?php
p2p_register_connection_type( array(
'name' => 'posts_to_pages',
'from' => 'post',
'to' => 'page',
'fields' => array(
'count' => array(
'title' => 'Count',
'type' => 'text',
),
'role' => array(
'title' => 'Role',
'type' => 'select',
'values' => array( 'engineer', 'support', 'manager' )
),
'special' => array(
'title' => 'Special',
'type' => 'checkbox'
),
'colors' => array(
'title' => 'Colors',
'type' => 'checkbox',
'values' => array(
'green'=>__('Green','my-textdomain'),
'yellow'=>__('Yellow','my-textdomain'),
'blue'=>__('Blue','my-textdomain'),
'white'=>__('White','my-textdomain') ),
'default' => 'blue'
),
)
) );
Each connection in the box will now have four additional columns:
- a 'Count' column, with a simple text input.
- a 'Role' column, with a dropdown with 3 possible values: engineer, support, and manager.
- a 'Special' column, with a single checkbox.
- a 'Colors' column, with several checkboxes.
The 'default' key allows you to pre-fill a field with a static value. But what if you wanted for example to fill it with the excerpt of one of the posts? Instead of a static value, you can pass a callback:
<?php
p2p_register_connection_type( array(
...
'fields' => array(
'blurb' => array(
'title' => 'Post Blurb',
'type' => 'textarea',
'default_cb' => 'my_get_blurb',
),
)
) );
function my_get_blurb( $connection, $direction ) {
global $post;
$key = ( 'from' == $direction ) ? 'p2p_to' : 'p2p_from';
$post = get_post( $connection->$key );
setup_postdata( $post );
return get_the_excerpt();
}
Ok, now you can input additional connection information, but how do you access it afterwards?
To display connection information, you use the connection id, which can be found in the $post
variable as the p2p_id
property:
<?php
while( $connected->have_posts() ) : $connected->the_post();
echo '<li>';
the_title();
// Display count and type
echo '<br>';
echo 'Count: ' . p2p_get_meta( get_post()->p2p_id, 'count', true );
echo '<br>';
echo 'Type: ' . p2p_get_meta( get_post()->p2p_id, 'type', true );
echo '</li>';
endwhile;
You might notice that p2p_get_meta()
looks very similar to the get_post_meta() function. The only difference is that, instead of a post id, you're using a connection id.
Finally, it's also possible to filter connections depending on what metadata they have.
<?php
$connected = new WP_Query( array(
'connected_type' => 'posts_to_pages',
'connected_items' => get_queried_object(),
'connected_meta' => array( 'type' => 'strong' )
) );
Or you could use the more advanced meta_query syntax:
<?php
$connected = new WP_Query( array(
'connected_type' => 'posts_to_pages',
'connected_items' => get_queried_object(),
'connected_meta' => array(
array(
'key' => 'count',
'value' => 5,
'compare' => '>',
'type' => 'numeric',
)
)
) );
This will get all connections which have the 'numeric' field value higher than 5.
You can use p2p_add_meta()
, p2p_update_meta()
and p2p_delete_meta()
.