Skip to content

Basic usage

Cristi Burcă edited this page Oct 7, 2013 · 51 revisions

In the following tutorial we will be creating connections between posts and pages and then displaying them in various ways.

Registering a connection type

The first thing you'll want to do is register a connection type. This will add a nice connection box to the post editing screen.

In your theme's functions.php file, add:

<?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' );
?>

If you call p2p_register_connection_type() directly from your functions.php file, it won't work. It needs to be placed in a function hooked to 'p2p_init', as above.

Go to the admin area and create a few connections.

Displaying connected pages/posts

Now, you will probably want to display these connections somewhere.

On a single post archive, we can use the get_queried_object() function to retrieve the current post being displayed.

The next block of code will display a list of pages that are connected from the current post:

<?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;
?>

We can also do the opposite: display posts that connect to the current page. The code is exactly the same, except it goes into a different file:

- // In single.php
+ // In page.php

- <h3>Related pages:</h3>
+ <h3>Related posts:</h3>

Using get_queried_object()

The reason why we can use the same code for both is because we use a special function called get_queried_object(), which is built into WordPress. It returns different results based on what type of archive you're currently on. For example:

  • if you're on a single post archive, it will return the post object
  • if you're on a single page archive, it will return the page object
  • if you're on a category archive, it will return the category object
  • etc.

There's a related function called get_queried_object_id(), which returns only the ID of the object, but uses the same logic as described above.

Using get_posts()

The above methods can be applied using the get_posts() function, with one extra argument - setting 'suppress_filters' to false. Example:

<?php
// Find connected posts
$connected = get_posts( array(
  'connected_type' => 'posts_to_pages',
  'connected_items' => get_queried_object(),
  'nopaging' => true,
  'suppress_filters' => false
) );

Working with an archive of posts

The above examples work for single post archives. If you would want to get a list of connections for each post in an archive, you can use each_connected().