Hello, I’m back with a new project and a question. I believe we discussed this before, but I cannot find the answer in my previous post.
We at the State Archives in Dubrovnik are working on a new project. I installed the most recent version of Tainacan, but I can’t find where to set the item navigation (previous/next at the bottom of the single item page) to follow title order instead of creation date.
I already set the default ordering to “Title / ASC” in the collection settings, but the navigation still follows creation date.
Is there a way to force the previous/next navigation to respect the title ordering? Thank you!
Recently in the release of version 1.0.0, we announced a new “Settings screen” for the plugin. That allows us to tweak default behavior of portions of the plugin and there is a section with “Default theme items list options”: Tainacan Wiki . In there you can choose Title instead of Creation Date. This will affect the Faceted Block, which is used by themes to render that Items list with filters. When a user clicks on an item from that list, the “previous/next” pagination will obey the context from which it came, including filters and sorting (BTW there was an issue to disable this behavior, it was also implemented in that same Settings screen).
The rule above can be override by three things: First is the same setting at the collection form. Second is the user preference. If a user is logged in and has already tweaked the sorting manually, that will be saved as his default sorting. Third is the URL. If the sorting parameter is there in the URL, that ones win.
You are talking about a the “next/previous” navigation, which is a component built by themes, not the plugin. Based on you previous project I believe you are using Blocksy. Our integration plugin filters the Blocksy buttons to make them obey the context in case the URL from where the item page is coming has parameters that explain that context (you can see how this works when you click an item coming from the Faceted Search vs. simply accessing the item page). It is the URL parameters that define that. However if you just access an item page without any extra information in its URL it will fallback to WordPress definition of “adjacent” posts and that is the creation date. There are however PHP hooks for tweaking that, such as `blocksy:post-navigation:previous-post`, but that would require a bit of code.
I get the point now… in case the URL is empty the WordPress default won’t be customizable, even by advanced themes like Blocksy. This is a fairly straightforward thing to do so I’ll add an issue to handle it:
Thx Mateus, I tried with this code and it works: (via php snippet)
// Override previous/next post navigation order for Tainacan items only.
// Regular WordPress posts will continue to navigate by date (default behavior).
function tainacan_nav_sort_by_title($orderby) {
global $post;
// Apply only to Tainacan post types (they always start with ‘tnc_col_’)
if ($post && strpos($post->post_type, ‘tnc_col_’) !== false) {
if (strpos($orderby, ‘DESC’) !== false) {
return ‘ORDER BY p.post_title DESC LIMIT 1’;
}
return ‘ORDER BY p.post_title ASC LIMIT 1’;
}
// For all other post types, keep the default (date-based) order
return $orderby;
}
function tainacan_nav_where_by_title($where, $in_same_term, $excluded_terms, $taxonomy, $post) {
if ($post && strpos($post->post_type, ‘tnc_col_’) !== false) {
global $wpdb;
$current_title = $post->post_title;
// Navigate to the item with the previous title (alphabetically)
if (strpos($where, ‘<’) !== false) {
return $wpdb->prepare(
“WHERE p.post_title < %s AND p.post_type = %s AND p.post_status = ‘publish’”,
$current_title, $post->post_type
);
}
// Navigate to the item with the next title (alphabetically)
return $wpdb->prepare(
“WHERE p.post_title > %s AND p.post_type = %s AND p.post_status = ‘publish’”,
$current_title, $post->post_type
);
}
// For all other post types, keep the default WHERE clause
return $where;
}