I need to give each post a unique post order for multiple Categories (per post) it is in. I can imagine a solution using custom fields whereby for each Category there is a corresponding custom field for the order.
E.g.
Categories:
Playlist1
Playlist2
Playlist3
Custom fields for the post:
Playlist1_Order
Playlist2_Order
Playlist3_Order
This method is obviously not easily scalable though so I would like to know if anybody has a more elegant solution.
Background
I am setting up a site to accompany a 24/7 broadcast. The playlist for the broadcast is a 12hr loop that is modified once per week. Some shows are added & removed and the order of some shows is re-arranged. I intend to define a custom post type of Shows and custom taxonomy of playlist. Each show will be added to one or more playlists. It must be possible for the show to have a unique order in each playlist.
(I decided to keep the question limited to posts & categories rather than post-types & taxonomies to avoid confusion).
UPDATE
To clarify, consider this example. There’s a post called “Rod’s Radio Roundup.” It’s in the categories “Tuesday Lineup” and “Wednesday Lineup.” On Tuesday it’s the third show and Wednesday it’s the 7th show. Later, it leaves Wednesday but moves to the 1st slot on Thursday and the 5th slot on Saturday. And there are 40 other posts like this.
The essential question: How do you maintain multiple orders, one per category, for a single post?
I recently overcame this same challenge. We needed a way to save a different sort order for each post in multiple categories.
There is an unused column in the wp_term_relationships table,
term_order
which defaults to 0 and is already cast as an integer.When a term is assigned to a post it gets an entry in this table for each term assigned. An object_ID (post_ID), a term_taxonomy_ID and the term order is set to 0.
Challenges:
The term_taxonomy_ID is sometimes different from the term_ID so you will have to know your term_taxonomy_IDs for each category.
Since the default is always 0 you need a method to set this at a higher number or all your new posts will always come first.
You will also need a way to query these posts since
term_order
is not part of the WP_Query class.You will also need a way to sort these posts. I will outline the Ajax drag and drop method I used but you could also use a metabox or custom field.
Ajax drag and drop sort:
You will need to create an admin options page and query for the posts and output them to a custom table or unordered list. You can also run this using Ajax so the user just has to select a category from a select box then load the posts from that category. (I will only be providing the Ajax and Php functions to save the order).
PHP wp_ajax function:
The jQuery Ajax:
How do we get our newly ordered posts?
posts_orderby filter:
Custom query API
You can run this function passing it an array of arguments similar to WP_Query and it will return an array of post_ids
Finally we just need to set new posts to have a term_taxonomy_id higher than the usual amount of posts to be ordered.
Still here?
This method has been tested and is used by a large news organization to set up the display order on the home page and category pages.
Things like this get complex really fast, this is maybe why the OP never returned because organizing live broadcasts using WP is not a great idea ( ad insertion, rotation rules, live changes on the fly, etc). Most of that stuff is better handled by a application that has a specific API based on those needs, and has XML/JSON feeds for throwing the data around if needed (like into WP).
The Order issue
This issue of ordering in general and drag and drop ordering seems to be a sore point that has been brought up in the past, with most of the core team agreeing that WordPress should remain chronological. Anything else should be a plugin feature for developers, the problem is there are a lot of people using WordPress as a development platform who run into this, WordPress is a CMS , right;)
So what does that leave us with?
Drag and drop is really intuitive so using something like jQuery UI’s
.sortable();
with a custom db entry or meta field works well. You can pretty much do the same thing as the default post gallery editor (and the menu editor), where you have an option to drag an drop the order , as well as manually enter a number and sort parameter (ASC, DESC) that you could extend to include name, or anyWP Query/WPDB
parameter.Anything more complex would have to be based on some sort of query rule system which would be pretty specific to the actual requirements at hand.
There are some plugins I have seen that do this, such as PostMash and Orderly, but nothing I know of that is really comprehensive, thus the need for hacking lots of template files.
I know you said “posts & categories”, but I think you’ll do better connecting posts to posts and storing the order as post metadata:
create a new post type,
show
(recommended, but you could usepost
instead)create a new post type,
playlist
(recommended, but you could usepost
instead)create a post/playlist, Tuesday Lineup, and save post metadata
shows = 8,73,5,68,994
(an ordered list of show IDs). (The ACF Relationship Field adds a great UI for this but the principle is the same without it.)
when displaying each post/playlist, read the value for
shows
, fetch those posts, and display them in order.To do this in EXTREME psuedo code you way want to try:
Note: with this method only define the “exceptions” with the
case
clauses and the “rule” with thedefault
.Simple. PHP for and WordPress get_posts() and a few checks will do the trick.
You can change
$args['orderby']
and$args['order']
to sort the way you need it to sort. View Order & Orderby ParametersHaven’t tested, but it should work!
OR
GET POSTS BY TAXONOMY SORT ORDER