Skip to content

Instantly share code, notes, and snippets.

@FinalDestiny
Created January 12, 2018 16:51
Show Gist options
  • Save FinalDestiny/006227be95330127520107f89c7e7ce9 to your computer and use it in GitHub Desktop.
Save FinalDestiny/006227be95330127520107f89c7e7ce9 to your computer and use it in GitHub Desktop.
add_action( 'pre_get_posts', 'et_custom_posts_per_page' );
function et_custom_posts_per_page( $query = false ) {
global $shortname;
if ( is_admin() ) {
return;
}
if ( ! is_a( $query, 'WP_Query' ) || ! $query->is_main_query() ) {
return;
}
if ( $query->is_category ) {
$query->set( 'posts_per_page', (int) et_get_option( $shortname . '_catnum_posts', '5' ) );
} elseif ( $query->is_tag ) {
$query->set( 'posts_per_page', (int) et_get_option( $shortname . '_tagnum_posts', '5' ) );
} elseif ( $query->is_search ) {
if ( isset( $_GET['et_searchform_submit'] ) ) {
$postTypes = array();
if ( !isset( $_GET['et-inc-posts'] ) && !isset( $_GET['et-inc-pages'] ) ) $postTypes = array('post');
if ( isset( $_GET['et-inc-pages'] ) ) $postTypes = array('page');
if ( isset( $_GET['et-inc-posts'] ) ) $postTypes[] = 'post';
$query->set( 'post_type', $postTypes );
if ( isset( $_GET['et-month-choice'] ) && $_GET['et-month-choice'] != 'no-choice' ) {
$et_year = substr( $_GET['et-month-choice'], 0, 4 );
$et_month = substr( $_GET['et-month-choice'], 4, strlen( $_GET['et-month-choice'] ) - 4 );
$query->set( 'year', absint( $et_year ) );
$query->set( 'monthnum', absint( $et_month ) );
}
if ( isset( $_GET['et-cat'] ) && $_GET['et-cat'] != 0 )
$query->set( 'cat', absint( $_GET['et-cat'] ) );
}
$query->set( 'posts_per_page', (int) et_get_option( $shortname . '_searchnum_posts', '5' ) );
} elseif ( $query->is_archive ) {
$posts_number = (int) et_get_option( $shortname . '_archivenum_posts', '5' );
if ( function_exists( 'is_woocommerce' ) && is_woocommerce() ) {
$posts_number = (int) et_get_option( $shortname . '_woocommerce_archive_num_posts', '9' );
}
$query->set( 'posts_per_page', $posts_number );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment