showposts
is an alias of posts_per_page
, which is a private query var, meaning it can’t be set from the query string. You could register your own query var and map that to posts_per_page
in a pre_get_posts
action. This could go in your theme’s functions.php
and would work for any main query.
// add a new query var to the list of known query vars
function wpd_custom_posts_queryvar( $vars ){
$vars[] = 'wpd-num';
return $vars;
}
add_filter( 'query_vars', 'wpd_custom_posts_queryvar' );
// hook pre_get_posts and set posts_per_page
// if it's the main query and our custom query var is set
function wpd_custom_posts_number( $query ){
if( $query->is_main_query() && $query->get( 'wpd-num' ) ){
$query->set( 'posts_per_page', $query->get( 'wpd-num' ) );
}
}
add_action( 'pre_get_posts', 'wpd_custom_posts_number' );