Order Post Type Navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Order Post Type Navigation | |
* | |
* @package optn | |
* @author Bernhard Kau | |
* @license GPLv3 | |
* | |
* @wordpress-plugin | |
* Plugin Name: Order Post Type Navigation | |
* Plugin URI: https://gist.github.com/2ndkauboy/64ddd0ba066208c8ea694f3cecf587ed | |
* Description: Order the Post Type Navigation by name. | |
* Version: 1.0.0 | |
* Author: Bernhard Kau | |
* Author URI: https://kau-boys.de | |
* Text Domain: optn | |
* License: GPLv3 | |
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt | |
*/ | |
/** | |
* Register a custom post type `sponsor`. | |
*/ | |
function optn_register_post_type() { | |
register_post_type( | |
'sponsor', | |
array( | |
'label' => __( 'Sponsor', 'optn' ), | |
'public' => true, | |
'has_archive' => true, | |
'show_ui' => true, | |
) | |
); | |
} | |
add_action( 'init', 'optn_register_post_type' ); | |
/** | |
* Order sponsors by name. | |
* | |
* @param WP_Query $query The WP_Query instance (passed by reference). | |
*/ | |
function optn_pre_get_posts_order_by_name( $query ) { | |
if ( is_admin() || ! $query->is_main_query() ) { | |
return; | |
} | |
if ( is_post_type_archive( 'sponsor' ) ) { | |
$query->set( 'orderby', 'name' ); | |
$query->set( 'order', 'ASC' ); | |
} | |
} | |
add_action( 'pre_get_posts', 'optn_pre_get_posts_order_by_name' ); | |
/** | |
* Set the `WHERE` clause to order adjacent partners by name. | |
* | |
* @param string $where The `WHERE` clause in the SQL. | |
* @param bool $in_same_term Whether post should be in a same taxonomy term. | |
* @param array $excluded_terms Array of excluded term IDs. | |
* @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. | |
* @param WP_Post $post WP_Post object. | |
* | |
* @return string | |
*/ | |
function optn_get_adjacent_post_where( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) { | |
if ( 'sponsor' !== $post->post_type ) { | |
return $where; | |
} | |
return preg_replace( '/p.post_date (.) ([^A-Z]+)/', "p.post_title $1 '{$post->post_title}' ", $where ); | |
} | |
add_filter( 'get_previous_post_where', 'optn_get_adjacent_post_where', 10, 5 ); | |
add_filter( 'get_next_post_where', 'optn_get_adjacent_post_where', 10, 5 ); | |
/** | |
* Set the `ORDER BY` clause to order adjacent partners by name. | |
* | |
* @param string $order_by The `ORDER BY` clause in the SQL. | |
* @param WP_Post $post WP_Post object. | |
* @param string $order Sort order. 'DESC' for previous post, 'ASC' for next. | |
* | |
* @return string | |
*/ | |
function optn_get_adjacent_post_sort( $order_by, $post, $order ) { | |
if ( 'sponsor' !== $post->post_type ) { | |
return $order_by; | |
} | |
return preg_replace( '/p.post_date/', 'p.post_title', $order_by ); | |
} | |
add_filter( 'get_previous_post_sort', 'optn_get_adjacent_post_sort', 10, 3 ); | |
add_filter( 'get_next_post_sort', 'optn_get_adjacent_post_sort', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog post for this snippet: https://kau-boys.com/2492/web-development/order-post-type-navigation-by-name