Skip to content

Admin box filters

scribu edited this page Apr 16, 2012 · 40 revisions

Ordering pages alphabetically

When creating connections, say you want to order pages alphabetically instead of chronologically in the 'View All' tab.

You can do this with a little bit of PHP:

<?php
function order_pages_by_title( $args, $ctype, $post_id ) {
	if ( 'posts_to_pages' == $ctype->name ) {
		$args['orderby'] = 'title';
		$args['order'] = 'asc';
	}

	return $args;
}

add_filter( 'p2p_connectable_args', 'order_pages_by_title', 10, 3 );

Only show top level pages

Or perhaps you'd like to only show top level pages in the admin box:

<?php
function order_pages_by_title( $args, $ctype, $post_id ) {
	if ( 'posts_to_pages' == $ctype->name ) {
		$args['post_parent'] = '0';
	}

	return $args;
}

add_filter( 'p2p_connectable_args', 'order_pages_by_title', 10, 3 );

Here are the filters which you can use for altering the admin boxes behaviour:

  • 'p2p_connectable_args' - called before querying for posts that the user can connect to the current post.
  • 'p2p_connected_args' - called before querying for posts that are already connected to the current post.
  • 'p2p_new_post_args' - called before creating a post via the "New" tab.

All of the above filters pass the follwing arguments to the callbacks:

  • array $args - the current arguments that are being filtered.
  • object $ctype - the current P2P_Connection_Type instance.
  • mixed $post_id - the current post we're dealing with

Show the box only for pages with a certain template

introduced in 1.3-alpha2

<?php
function filter_box_page_template( $show, $post, $ctype ) {
	if ( 'YOUR_CONNECTION_TYPE' == $ctype->name ) {
		return ( 'YOUR-TEMPLATE.php' == $post->page_template );
	}

	return $show;
}

add_filter( 'p2p_admin_box_show', 'filter_box_page_template', 10, 3 );