Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sort all posts on all archives alphabetically.
<?php
/**
* Alphabetical Archives
*
* @package alphabetical-archives
* @author Bernhard Kau
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: Alphabetical Archives
* Plugin URI: https://gist.github.com/2ndkauboy/96e9c11dc36aa40a762032cf7d040fb0
* Description: Sort all posts on all archives alphabetically.
* Version: 1.0.0
* Requires at least: 5.0
* Requires PHP: 5.6
* Author: Bernhard Kau
* Author URI: https://kau-boys.de
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: alphabetical-archives
* Domain Path: /languages
*/
/**
* Sort all posts on all category archives alphabetically.
*
* @param WP_Query $query The query.
*
* @return
*/
function aa_sort_all_archives( $query ) {
// Only sort main query.
if ( ! $query->is_main_query() ) {
return;
}
// Only sort category archives.
if ( ! is_category() ) {
return;
}
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'post_title' );
}
add_action( 'pre_get_posts', 'aa_sort_all_archives' );
/**
* Sort all posts on all category archives alphabetically.
*
* @param WP_Query $query The query.
*
* @return
*/
function aa_sort_alphabetical_archives( $query ) {
// Only sort main query.
if ( ! $query->is_main_query() ) {
return;
}
// Only sort category archives.
if ( ! is_category( 'alphabetical' ) ) {
return;
}
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'post_title' );
}
// Comment this hook in (and the other one out) if you only want to order by a specific category slug
#add_action( 'pre_get_posts', 'aa_sort_alphabetical_archives' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment