Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change posts per page for post type
<?php
/**
* Change 'products' posts per page on archive
* This is the long version that explains everything
*/
function be_modify_products_query_long ( $query ) {
// Check to make sure we're modifying the main query
global $wp_the_query;
if( $wp_the_query !== $query )
return;
// Make sure we're not in the admin area
if( is_admin() )
return;
// Make sure we are on the post type archive query
if( is_post_type_archive( 'products' ) ) {
$query->set( 'posts_per_page', '8');
}
}
/**
* Change 'products' posts per page on archive
* This is the short version we actually use
*/
function be_modify_products_query( $query ) {
global $wp_the_query;
if( $wp_the_query === $query && !is_admin() && is_post_type_archive( 'products' )
$query->set( 'posts_per_page', '8' );
}
add_action( 'pre_get_posts', 'be_modify_products_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.