The next version of Index is here! To celebrate, take 25% off your purchase with the code 25OFF. Learn more
Index Logo

Sorting fields

Ordering fields using the single and multi edit field registration hooks is almost impossible due to the ability to call those hooks in any plugin, at any time. This resource separates the sort order and registration logic. All registered fields are included before this resource is fired.

Modifying the sort order

Each sort order is tied to a specific post type:

// replace {$type} with the post type name
add_filter("index_{$type}_order_edit_fields", function($fields) {
    return $fields;
});

Sort order options

The $fields parameter in the hook is a simple array of all edit field ids, depending on whether one item or multiple items are being edited. The global $Index class includes four methods for sorting which abstract complex PHP array sorting and also verify the item exists in the current list (if not, it ignores the update):

placeFirst

Places the field at the beginning of the list:

$Index->placeFirst(
	$fields,        // array of all field ids
	'_thumbnail_id' // field id to move
);

placeLast

Places the field at the end of the list:

$Index->placeLast(
	$fields,        // array of all field ids
	'_thumbnail_id' // field id to move
);

placeAt

Places the field at a specific position in the list (note this is only advised if you know exactly what you’re doing):

$Index->placeAt(
	$fields,        // array of all field ids 
	2,              // new position
	'_thumbnail_id' // field id to move
);

placeAfter

Places the field after another field, if they both exist:

$Index->placeAfter(
	$fields,        // array of all field ids 
	'title',        // anchor field id
	'_thumbnail_id' // field id to move
);

Example

add_filter("index_post_order_edit_fields", function($fields) {
	global $Index;  
	
	$Index->placeAfter(
		$fields,
		'title',
		'_thumbnail_id'
	);

	return $fields;
});

Resources

Get started with Index

See for yourself why Index is the most intuitive, beautiful interface for managing your WordPress content.