set posts per page

Solutions on MaxInterview for set posts per page by the best coders in the world

showing results for - "set posts per page"
Hamza
25 Jul 2019
1global $wp_query;
2$wp_query->set('posts_per_page', 1);
3$wp_query->query($wp_query->query_vars);
4
Domenico
17 Mar 2017
1add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
2function myprefix_adjust_offset_pagination($found_posts, $query) {
3
4    //Define our offset again...
5    $offset = 10;
6
7    //Ensure we're modifying the right query object...
8    if ( $query->is_home() ) {
9        //Reduce WordPress's found_posts count by the offset... 
10        return $found_posts - $offset;
11    }
12    return $found_posts;
13}
14
Abigail
27 Mar 2019
1add_action('pre_get_posts', 'myprefix_query_offset', 1 );
2function myprefix_query_offset(&$query) {
3
4    //Before anything else, make sure this is the right query...
5    if ( ! $query->is_home() ) {
6        return;
7    }
8
9    //First, define your desired offset...
10    $offset = 10;
11    
12    //Next, determine how many posts per page you want (we'll use WordPress's settings)
13    $ppp = get_option('posts_per_page');
14
15    //Next, detect and handle pagination...
16    if ( $query->is_paged ) {
17
18        //Manually determine page query offset (offset + current page (minus one) x posts per page)
19        $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
20
21        //Apply adjust page offset
22        $query->set('offset', $page_offset );
23
24    }
25    else {
26
27        //This is the first page. Just use the offset...
28        $query->set('offset',$offset);
29
30    }
31}
32
Ian
26 Jul 2018
1function wpsites_exclude_latest_post( $query ) {
2	if ( $query->is_home() && $query->is_main_query() ) {
3		$query->set( 'offset', '1' );
4	}
5}
6
7add_action( 'pre_get_posts', 'wpsites_exclude_latest_post', 1 );
8