Number of pages – multiple (custom) post types

Milo is right, you should be using pre_get_posts instead of calling a 2nd query on load. This will make paging much easier. Whatever the orderby session you’re using it should still be accessible in the hook. The code to do so is fairly straightforward too, it would look like this:

/**
 * Modify the query before it gets loaded
 *
 * @param WP_Query Object $query
 *
 * @return void
 */
function wpse_228230( $query ) {

    if( $query->is_admin ) {
        return;
    }

    // Your archive conditional here
    if( is_post_type_archive() ) {
        $query->set( 'post_type', array( 'post', 'cust_article' ) );
        $query->set( 'posts_per_page', 15 );
        $query->set( 'orderby', array( 'title' => 'DESC' ) );
    }

}
add_action( 'pre_get_posts', 'wpse_228230' );

This will modify the main query before it hits the database. Otherwise WordPress will get the natural query, hit your custom WP_Query and ping the database again which is unnecessary overhead.

Leave a Comment