Skip to content

Instantly share code, notes, and snippets.

@zedejose
Last active March 18, 2018 14:34
Show Gist options
  • Save zedejose/19e62d8ba30fd77d2677b7c97f8b6de6 to your computer and use it in GitHub Desktop.
Save zedejose/19e62d8ba30fd77d2677b7c97f8b6de6 to your computer and use it in GitHub Desktop.
<?php
/*
* Order posts by title
*
* @see https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
*
* Notes:
* - 'zewpgists_' is a prefix for all my gists. Replace with your own.
*
*/
add_action( 'pre_get_posts', 'zewpgists_order_posts_by_title' );
/**
* Order the main query by title
*
* @param object $query Main query.
*
* @return object Reordered query.
*/
function zewpgists_order_posts_by_title( $query ) {
/*
* Apply only to the main query and on the main posts loop
*
* @see https://codex.wordpress.org/Function_Reference/is_main_query
* @see https://codex.wordpress.org/Function_Reference/is_front_page
*/
if ( $query->is_main_query() && $query->is_front_page() ) {
$query->set( 'orderby', 'title' ); // Default is post_date.
$query->set( 'order', 'ASC' ); // Default is DESC.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment