Writing vs. Programming

I just saw on twitter a time lapsed video of the author Scott Berkun writing 1000 words:

While watching it, I was amazed at how much it resembles the way I program.

In spite of the resemblance, I tend to think writing is harder, because there’s no computer to tell you if what you wrote works or not.

In both cases you can ask other people to review your work, sure, but there’s no comparable tool for writing that could match the speed of a web page reload or even of a compiler.

I should write more.

Advanced Metadata Queries

WordPress 3.1 will come with powerful taxonomy querying capabilities.

It will also have comparable capabilities for querying posts based on various custom fields.

Scenario

You have a ‘product’ custom post type with ‘price’ and ‘description’ custom fields.

Before

Here is the most advanced meta query that you could do out of the box, before WP 3.1:

Get all the products with a price greater than 100:

query_posts( array(
  'post_type' => 'product',
  'meta_key' => 'price',
  'meta_value' => 100,
  'meta_compare' => '>'
) );

After

Here is what you can do now:

Get all the products with a price between 100 and 200 and a description that doesn’t contain the string ’round’:

query_posts( array(
	'post_type' => 'product',
	'meta_query' => array(
		array(
			'key' => 'price',
			'value' => array( 100, 200 ),
			'compare' => 'BETWEEN',
			'type' => 'numeric',
		),
		array(
			'key' => 'description',
			'value' => 'round',
			'compare' => 'NOT LIKE'
		)
	)
) );

A breakdown of the enhancements:

  • more than one meta query possible (#14645)
  • several new ‘compare’ values: ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’
  • ‘type’ parameter

A complete reference can be found in the Codex: WP_Query#Custom_Field_Parameters

Having read this, please don’t use custom fields as taxonomies.

Front-end Editor: Version 1.9.2

Thought I’d do another release before year’s end. It is focused on improving the existing features rather than adding new stuff.

The tooltip looks sexier now. It resembles the one on the iPhone:

tooltip

When you click a link inside the rich editor, a gmail-like tooltip will appear:

link tooltip

Another usability improvement is limiting the maximum height of the wysiwyg editor to the window height, so that the controls are always visible. This way, you don’t have to constantly scroll up and down from the text you’re editing.

For developers: I’ve moved plugin development to github:

https://github.com/scribu/wp-front-end-editor

Inserting a banner between posts

It’s pretty easy to inject content between posts if you have direct access to The Loop in the theme:

$counter = 0;
 
while ( have_posts() ) : the_post();
 
	if ( 1 == $counter )
		echo '<div>Some banner</div>';
 
	$counter++;
 
	// the_title() etc.
 
endwhile;

If you want to do this without modifying the theme, the solution is not so obvious, but it exists:

function insert_between_posts( $post ) {
	global $wp_query;
 
	// Check if we're in the main loop
	if ( $wp_query->post != $post )
		return;
 
	// Check if we're at the right position
	if ( 1 != $wp_query->current_post )
		return;
 
	// Display the banner
	echo '<div>Some banner</div>';
}
add_action( 'the_post', 'insert_between_posts' );

Note that with this second approach, the banner will be displayed throughout the site. Fortunately, this can be easily controlled using conditional tags:

function insert_between_posts( $post ) {
	global $wp_query;
 
	// Check if we're in the right template
	if ( ! is_home() )
		return;
 
	// Check if we're in the main loop
	if ( $wp_query->post != $post )
		return;
 
	// Check if we're at the right position
	if ( 1 != $wp_query->current_post )
		return;
 
	// Display the banner
	echo '<div>Some banner</div>';
}
add_action( 'the_post', 'insert_between_posts' );

I hope this gives you some ideas for your next great plugin or child theme. Happy hacking!

Posts 2 Posts: Version 0.5

This release is focused on enhancing the API, making it easier to leverage the p2pmeta table.

First of all, a new variable for WP_Query is now available: connected_meta. It allows you to restrict connections based on what meta data they have. Here’s an example:

$my_query = new WP_Query( array(
    'post_type' => 'book',
    'connected_to' => 'any',
    'connected_meta' => array(
        'connection_date' => 'long ago',
    )
) );

This will retrieve any book that has a connection to any other post. Also, the connections have to have a custom field with the key connection_date and the value long ago.

And, if that’s not enough, you can use the advanced meta query syntax:

$my_query = new WP_Query( array(
    'post_type' => 'book',
    'connected_to' => 'any'
    'connected_meta' => array(
        array(
            'key' => 'connection_date',
            'value' => array( 'long ago', 'yesterday' ),
            'compare' => 'IN'
        )
    )
) );

Also, after you do the query, you can access the rest of the connection meta data via the p2p_id property, assigned to each found post:

while ( $my_query->have_posts() ) : $my_query->the_post();
 
    $connection_type = p2p_get_meta( $post->p2p_id, 'connection_type', true );
 
endwhile;

Managed WP Hosting

There seem to be a lot of hosting services popping up, dedicated specifically to websites built on WordPress.

Until recently, you basically had two choices:

Blogging services: Go to wordpress.com and make a blog. They take care of everything; you just have to come up with the content. The only problem would be that you are limited to a certain set of themes and plugins.

Self hosting: Buy some server space and install WordPress. You can install any plugin or theme, but you also have to take care of updates, security and scaling.

Managed hosting for WordPress fills the gap between the two. You still get to install any plugin or theme, but you leave the sysadmin headaches to them.

So here are some of these hosting providers that I’ve heard of:

page.ly – They seem to be the first ones that offered specialized WordPress hosting.

PressLabs – A newly announced service, operated by the guys behind blogu.lu, a Romanian blogging network.

wpengine.com – Also a relatively new service.

And of course, let’s not forget the mac daddy of all WordPress hosting, vip.wordpress.com.

I take this as a sign of the WP ecosystem maturing, as users are willing to spend a little more to have their sites set up and maintained properly.

WordCamp Romania 2010

Today I spoke at the second edition of WordCamp Romania.

My presentation was about the upcoming features in WordPress 3.1. I was a lot more relaxed this year during my talk so it went pretty well.

Speakers were more focused on WordPress than in the previous edition and the food was much better, so we’re definitely making progress. Kudos to the organisers!

If you weren’t able to attend, here’s a nice summary (in Romanian).

Plugin Dependencies: Version 1.0

The idea of dependencies for WordPress plugins has been floating around for some time: #10190, #11308, #12612.

Although plugins can degrade gracefully by using actions and filters, there are some things they can’t do, due to the simple fact that they need to be activated first. Such as:

Activation prevention

Cascade deactivation

My hope is that this meta-plugin will be rolled into Core at some point. Until then, we can learn more rapidly what works and what doesn’t.

Go to the wp.org page to see how easy it is to define a dependency.

Posts 2 Posts: Version 0.4

Using a taxonomy to store post-to-post connections was an interesting exercise. It worked, but when it came time to extend it, it felled flat on its face.

That’s why connections are now stored in a custom table, with an additional meta table for adding arbitrary information per connection.

It is now possible to connect the same two posts twice, but with different metadata, something that would be needed in Justin Tadlock’s movies scenario, for example.

The procedure for migrating old connections is the same as in the previous version: go to /wp-admin/?migrate_p2p and you’re done. (Make sure you have a recent database backup before you do this.)

When searching for posts to connect, it will search only by post title and not by content, as in previous versions.

Reciprocal connections are now stored only once.

Speaking of reciprocal connections, most function in the API accepted a the third parameter, which used to be $reciprocal = false. It has been replaced with $data = array(). More info can be found in the file api.php.

Unless you’re doing advanced stuff, you shouldn’t need to touch the API, though. You can just use WP_Query or get_posts():

Get posts connected from a particular post: $post_id -> $posts

$connected = get_posts( array(
  'suppress_filters' => false,
  'post_type' => 'book',
  'connected_from' => $post_id,
) );

Get posts connected to a particular post: $post_id <- $posts

$connected = get_posts( array(
  'suppress_filters' => false,
  'post_type' => 'book',
  'connected_to' => $post_id,
) );

Get connected posts, regardless of direction: $post_id <- $posts OR $post_id -> $posts

$connected = get_posts( array(
  'suppress_filters' => false,
  'post_type' => 'book',
  'connected' => $post_id,
) );

It’s a story about us, people, being persuaded to spend money we don’t have, on things we don’t need, to create impressions that won’t last, on people we don’t care about.

Tim Jackson