Skip to content

Instantly share code, notes, and snippets.

@rfmeier
Created January 8, 2016 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfmeier/3614971908b7be747a70 to your computer and use it in GitHub Desktop.
Save rfmeier/3614971908b7be747a70 to your computer and use it in GitHub Desktop.
Use the custom archive setting to modify the number of posts on a custom post type archive page.
<?php
/**
* Use the custom archive setting to modify the number of posts on a custom post type archive page.
*/
add_action( 'pre_get_posts', 'cpt_as_pre_get_posts' );
/**
* Callback for WordPress 'pre_get_posts' action.
*
* Modify the number of posts on an testimony archive page if set to a custom value.
*
* @see https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
*
* @since 0.0.1
*
* @return void
*/
function cpt_as_pre_get_posts( $query ) {
//* if admin or not on the testimony archive page, exit
if ( is_admin() || ! is_post_type_archive( 'testimony' ) ) {
return;
}
//* get the custom posts_per_page value
$posts_per_page = (int) genesis_get_cpt_option( 'posts_per_page', 'testimony' );
//* if a valid posts_per_page value and on the main query, adjust the query
if ( $posts_per_page && $query->is_main_query() ) {
$query->set( 'posts_per_page', $posts_per_page );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment