Skip to content

Instantly share code, notes, and snippets.

@davidperezgar
Created April 15, 2015 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidperezgar/005e650e1d28018ce4b2 to your computer and use it in GitHub Desktop.
Save davidperezgar/005e650e1d28018ce4b2 to your computer and use it in GitHub Desktop.
Posts 2 posts functions for WordPress
<?php
//In functions .php
function my_connection_types() {
p2p_register_connection_type( array(
'name' => 'posts_to_pages',
'from' => 'post',
'to' => 'page'
) );
}
add_action( 'p2p_init', 'my_connection_types' );
?>
<?php
// In Single.php
// Find connected pages
$connected = new WP_Query( array(
'connected_type' => 'posts_to_pages',
'connected_items' => get_queried_object(),
'nopaging' => true,
) );
// Display connected pages
if ( $connected->have_posts() ) :
?>
<h3>Related pages:</h3>
<ul>
<?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
// Prevent weirdness
wp_reset_postdata();
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment