Sarathlal N

Update query order - WordPress

The WordPress post type archives are ordered by date, descending by default. This is the best & suitable listing method for a blogging platform.

In a recent project, I have to use WordPress default archive pages to list out posts in a custom post type. As per the default nature, the WordPress will list all posts in that custom post type as per the order of published date. But client need to list posts based on post title in ascending order.

If the content chronology isn’t important, there is no sense for a date wise listing.

In such cases, we can reorder the posts using pre_get_posts filter hook. Also we can use conditional tag to specify the archives.

Sort blog page listing by title

add_action( 'pre_get_posts', 'foo_modify_query_order' );
function foo_modify_query_order( $query ) {
	if ( $query->is_home() && $query->is_main_query() ) {
		$query->set( 'orderby', 'title' );
		$query->set( 'order', 'ASC' );
	}
}

Sort archive page listing by menu order

add_action( 'pre_get_posts', 'bar_modify_query_order'); 
function bar_modify_query_order($query){
    if(is_archive()) {
       $query->set( 'orderby', 'menu_order' );       
       $query->set( 'order', 'ASC' );
    }    
};

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Automating Code Linting with GitHub Actions for WordPress Plugins
  2. Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
  3. The Ultimate Guide to Indexing in Database Design
  4. Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
  5. A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.