Sample of to use pre_get_posts hook.
<?php
// Load our function when hook is set
add_action( 'pre_get_posts', 'rc_modify_query_limit_posts' );
function rc_modify_query_limit_posts( $query ) {
// Check if on frontend and main query is modified
if( ! is_admin() && $query->is_main_query() ) {
$query->set('posts_per_page', '5');
}
}