number_posts
is not a valid parameter in pre_get_posts
, you should be using posts_per_page
You should also include a check (!is_admin()
) in your query to check whether you are on the front end or back end as pre_get_posts
alters back end queries as well
Rewrite your code to the following:
add_action( 'pre_get_posts', function ( $query )
{
if ( !is_admin()
&& $query->is_main_query()
&& $query->is_tax()
) {
$query->set( 'posts_per_page', '20' );
$query->set( 'orderby', 'rand' );
}
});