Posts 2 Posts: Version 1.3

Improved API

In this release, I focused on making the API more uniform; everything works for both posts-to-posts and posts-to-users connections now. This is possible because you can pass whole post and user objects, instead of just IDs.

Previously:

$connected = get_users( array(
	'connected_type' => 'posts_to_users',
	'connected_items' => $post->ID
) );

Now:

$connected = get_users( array(
	'connected_type' => 'posts_to_users',
	'connected_items' => $post
) );

This small change gives P2P more information to work with, which means fewer database queries are needed.

Shortcodes!

The plugin now comes with a pair of shortcodes for conveniently listing connected items anywhere.

Say you defined a connection type called ‘posts_to_pages’. Here’s how the shortcode would look like:

[p2p_connected type=posts_to_pages]

If you insert that into a post, it will display a list of connected pages. If you insert it into a page, it will display a list of connected posts.

And there’s one for related posts too:

[p2p_related type=posts_to_pages]

Shortcodes and widgets also have filters for modifying the HTML output.

Default field values

You can now set default values for the connection fields in the metabox, likes so:

p2p_register_connection_type( array(
	'name' => 'posts_to_pages',
	'from' => 'post',
	'to' => 'page',
 
	'fields' => array(
		'color' => array(
			'title' => 'Color',
			'type' => 'checkbox',
			'values' => array( 'green', 'yellow', 'blue', 'white' ),
			'default' => 'blue'
		),
	)
) );

In total, there were 16 resolved issues in this release.

Always-on/always-on-you devices provide three powerful fantasies: that we will always be heard; that we can put our attention wherever we want it to be; and that we never have to be alone.

Sherry Turkle

How To Become A WordPress Guru

tl;dr: Read the effin’ source!

If a function or a parameter isn’t documented on the Codex, that doesn’t mean it doesn’t exist. Look for it in the source code. Doubly so for actions and filters.

When a template tag doesn’t behave properly, don’t just jump over to WordPress StackExchange. Dig up it’s definition and try to figure it out for yourself first.

If your job title includes the word “WordPress” + any of “developer”, “freelancer”, “ninja”, “rockstar”, “mutant turtle”, jumping into Core code should feel as natural as breathing.

Set up your development environment so that it’s only one click or a few keystrokes away.

Resetting Your Github Fork

Let’s say I want to contribute to a project on github. The project repository is at wp-cli/wp-cli. First I fork it, and then clone the resulting repository, scribu/wp-cli:

git clone --recursive git@github.com:scribu/wp-cli.git
cd wp-cli

Now, I make some commits to master, push them to my fork and open a pull request. Piece of cake:

git commit -m "awesome new feature"
git push

But, what happens if my pull request is rejected or only certain commits are accepted? I’m left with a dirty master branch. Oh noes!

There are two ways of solving this:

A. Delete my fork and create it again via the github interface. Can’t get any easier than that.

B. Use git reset:

git remote add upstream git://github.com/wp-cli/wp-cli.git
git fetch upstream
git branch backup
git reset --hard upstream/master
git push --force

If I made a mistake, I can rescue my commits by calling git checkout backup.

Posts 2 Posts: Version 1.2

The main attraction in this release is the new admin screen that gives an overview of all the connection types found in the system:

Connection Types screen

Another thing to note is that queries like this no longer work:

$query = new WP_Query( array(
  'connected_items' => get_queried_object_id()
) );

You have to set the ‘connected_type’ parameter:

$query = new WP_Query( array(
  'connected_type' => 'posts_to_pages',
  'connected_items' => get_queried_object_id()
) );

Also, you can pass an array of connection types and P2P will pick the ones that make sense.

$query = new WP_Query( array(
  'connected_type' => array( 'posts_to_pages', 'actors_to_movies' ),
  'connected_items' => get_queried_object_id()
) );

As with every release, it contains a bunch of smaller enhancements and bug fixes. See the changelog for more details.

Plugin Dependencies: Version 1.2

In previous versions of the plugin, to define a depedency to BuddyPress, you’d have to write:

Depends: buddypress/bp-loader.php

As a user pointed out, using the plugin path like that is not a good idea.

It turns out that there’s a much more stable piece of information that can be used to identify plugins with: their name. D’oh!

So now you can just write this instead:

Depends: BuddyPress

Besides being a lot more user-friendly, since plugin names rarely change, it’s more reliable as well. Win!

Of course, for super-formal specs, the Provides: header still works.

Management Style

In 2004, Linus Torvalds wrote a characteristically honest guide on how to handle technical decisions for the Linux Kernel.

Just replace “kernel manager” with “bug gardener” and you’ve got a nice handbook on how to manage WordPress tickets and patches.

Github Tricks

What I’m about to show you are functionalities that I’ve actually needed at one point or another, but which aren’t featured proeminently in the github interface.

In both cases, start at a repository’s home page. For example:

https://github.com/andreascreten/wp-cli

See commits from a certain user

  1. Click the Stats & Graphs button.
  2. Click the Contributors tab.
  3. Click the name of the contributor you’re interested in.

You’ll end up with an URL like this:

https://github.com/andreascreten/wp-cli/commits/master?author=scribu

See issues assigned to a certain user

In Github Issues, it’s really easy to see tickets assigned to yourself, but no apparent way to view tickets assigned to others. There is a method, but it’s rather convoluted:

  1. Click the Issues button.
  2. Do a random search using the Issues & Milestones search box.
  3. Select “Assigned to” user from the dropdown on the left.

The final URL will be:

https://github.com/andreascreten/wp-cli/issues?assignee=andreascreten&state=all

For more hidden gems, see GitHub Secrets.

The joke is actually a jump from one context to another. [...] All creativity is an extended form of a joke.

Alan Kay

In software systems, it is often the early bird that makes the worm.

Alan Perlis