You can add custom query variables to WordPress with add_query_var()
.
global $wp;
$wp->add_query_var( 'includes' );
Having added the query var you would check if it is passed with get_query_var('includes')
, then you could use WP_Query()
to get the pages (You can see the available args here).
If it’s omitted you would simply run a query to get all children of the page.
Another nice addition would be to add the query var to the permalinks using add_rewrite_rules
.
function custom_query_var() {
global $wp;
add_rewrite_rule( '^books/(.+)', 'index.php?pagename=books&includes=$matches[1], 'top' );
}
add_action( 'init', 'custom_query_var' );
You would need to flush these rules if you switch to a different theme.
function flush_custom_query_var() {
flush_rewrite_rules();
}
add_action( 'after_theme_switch', 'flush_custom_query_var' );
I haven’t actually tested the rewrite rules when passing an array of parameters instead of just a single one, so it would need some alteration.