Wordpress - Should category.php and The Loop be used if the query needs to be customizable?
Yes, the loop and a specific category template should be used even if you want to customize the query. Why?:
Even with a custom page, the main query will run. So with a custom page, you are not actually avoiding the main query, you are only replacing it with a different query.
The main query itself is customizable.
If you deviate from the default WordPress behaviour, it'll be difficult for you to maintain in the future, especially it'll be difficult for other developers in case someone else takes over your work in the future.
How to modify the main query:
Fortunately, WordPress is extremely customizable, that means the main query (the loop) is also customizable. You may use the pre_get_posts
action hook or query_posts()
function to alter the main query. However, it's recommended to use the pre_get_posts
hook.
For example, say you want to change the order of posts in a category based on ascending order of date. For that you may use the following CODE in your theme's functions.php
file:
add_action( 'pre_get_posts', 'wpse258109_customize_category_query' );
function wpse258109_customize_category_query( $query ) {
if( ! is_admin() && $query->is_main_query() && $query->is_category( 'your-category-slug' ) ) {
// get the orderby value from where ever you want and set in the main query
$query->set( 'orderby', array( 'date' => 'ASC' ) );
}
}
This CODE will change the default behaviour of the main query and your category posts for your-category-slug
archive page will load in ascending order. Of course you can make any change to this main query as you can with any custom query using the WP_Query class.
First of all: Great you're thinking about a wasted query! :)
Second: WP always runs a main query no matter what you do. But what you can do is alter this query instead of ignoring it and creating a secondary loop. This can be done using the pre get posts
hook.