Skip to content

Instantly share code, notes, and snippets.

@LeanSeverino1022
Created April 5, 2020 04:29
Show Gist options
  • Select an option

  • Save LeanSeverino1022/1cc8bd58faa026f5441dae52b59d6b7f to your computer and use it in GitHub Desktop.

Select an option

Save LeanSeverino1022/1cc8bd58faa026f5441dae52b59d6b7f to your computer and use it in GitHub Desktop.
[Change Posts Per Page] #wordpress

Change Posts Per Page Let’s say you have a custom post type called Event. You’re displaying events in three columns, so instead of the default 10 posts per page you want 18. If you go to Settings > Reading and change the posts per page, it will affect your blog posts as well as your events.

We’ll use pre_get_posts to modify the posts_per_page only when the following conditions are met:

On the main query Not in the admin area (we only want this affecting the frontend display) On the events post type archive page

/**
 * Change Posts Per Page for Event Archive
 * 
 * @author Bill Erickson
 * @link https://www.billerickson.net/customize-the-wordpress-query/
 * @param object $query data
 *
 */
function be_change_event_posts_per_page( $query ) {
	
	if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'event' ) ) {
		$query->set( 'posts_per_page', '18' );
	}

}
add_action( 'pre_get_posts', 'be_change_event_posts_per_page' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment