How to order entries by title on Portfolio CPT archive in WordPress

Posted on

In the members-only forum, a user asked:

I use Genesis Portfolio Pro on this site: http://weinschardt.de/portfolio
I have tried different Plugins to sort the Portfolio but nothing works.

How can I get the Portfolio to sort alphabetically?

The default ordering of entries for any post type in WordPress is by published date in descending order.

pre_get_posts action hook can be used to alter this sort order easily.

Adding the following in child theme’s functions.php will display posts on portfolio Custom Post Type archive page in ascending order of their titles.

add_action( 'pre_get_posts', 'sk_change_portfolio_posts_order' );
/**
 * Order entries by title on Portfolio CPT archive.
 *
 * @link http://www.billerickson.net/customize-the-wordpress-query/
 * @param object $query data
 *
 */
function sk_change_portfolio_posts_order( $query ) {
    if ( $query->is_main_query() && !is_admin() && is_post_type_archive( 'portfolio' ) ) {
        $query->set( 'order', 'ASC' );
        $query->set( 'orderby', 'title' );
    }
}