Make WordPress Core

Opened 16 years ago

Closed 16 years ago

Last modified 3 years ago

#5857 closed enhancement (fixed)

an object's terms should be positionable

Reported by: andy's profile andy Owned by: ryan's profile ryan
Milestone: 2.5 Priority: normal
Severity: normal Version:
Component: General Keywords: taxonomy sort order has-patch
Focuses: Cc:

Description

There is no way for object term entry order to dictate term display order.

Example: register a taxonomy "People" for listing people in photos. You want to list them from left to right but the taxonomy system can't do that.

A plugin could do it by storing in postmeta the terms in entry order and filtering the output, but it could be made a core feature with a little effort.

Add a column in the term_relationships table. Call it term_order. When terms are saved into taxonomy registered with 'sort'=>'term_order' we can do something like this:

INSERT INTO term_relationships (object_id, term_taxonomy_id, term_order) VALUES (1, 1, 1), (1, 2, 2), (1, 8, 3) ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)

Attachments (1)

5857.diff (1.8 KB) - added by andy 16 years ago.

Download all attachments as: .zip

Change History (5)

@andy
16 years ago

#1 @andy
16 years ago

  • Keywords has-patch added
  • Owner changed from anonymous to ryan

#2 @ryan
16 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [6851]) Support ordering for term relationships. Props andy. fixes #5857

#3 @darkdragon
16 years ago

  • Milestone changed from 2.6 to 2.5

#4 @lgedeon
13 years ago

  • Cc luke.gedeon@… added

Based on the code above (which is now in core), here is the code to use term_order to sort tags. It can, of course, be adapted for other taxonomies.

/**
 * Sort post_tags by term_order
 *
 * @param array $terms array of objects to be replaced with sorted list
 * @param integer $id post id
 * @param string $taxonomy only 'post_tag' is changed.
 * @return array of objects
 */
function plugin_get_the_ordered_terms ( $terms, $id, $taxonomy ) {
	if ( 'post_tag' != $taxonomy ) // only ordering tags for now but could add other taxonomies here.
		return $terms;

	$terms = wp_cache_get($id, "{$taxonomy}_relationships_sorted");
	if ( false === $terms ) {
		$terms = wp_get_object_terms( $id, $taxonomy, array( 'orderby' => 'term_order' ) );
		wp_cache_add($id, $terms, $taxonomy . '_relationships_sorted');
	}

	return $terms;
}

add_filter( 'get_the_terms', 'plugin_get_the_ordered_terms' , 10, 4 );

/**
 * Adds sorting by term_order to post_tag by doing a partial register replacing
 * the default
 */
function plugin_register_sorted_post_tag () {
	register_taxonomy( 'post_tag', 'post', array( 'sort' => true, 'args' => array( 'orderby' => 'term_order' ) ) );
}

add_action( 'init', 'plugin_register_sorted_post_tag' );

?>

To use this, you will need to add tags in the order you want them to be and if you get them out of order you may have to remove some and add them back correctly. I might write a better UI later, but for now this at least works.

I guess next actions would be to write a patch for object ordering, and to come up with a UI(s) for both tag-style and category-style term_ordering.

Note: See TracTickets for help on using tickets.