Plugins & Themes

change number of items per page on CPT in Dashboard

2 Replies ·

  1. Hi Justin, in my Stargazer child theme I would like to change the number of post results that appear in the main list of each CPT I have created when I click on that CPT in the left menu of the Dashboard.

    This is not effected by changing the “Settings/Reading/Blog pages show at most” on the Dashboard, or changing the “Number of items per page:” in the Screen Options tab, either.

    I have a custom functions plugin that I created with your help. The line $query->set( 'posts_per_page', 5 ); was added by me to control the number of post results that show up on pages that visitors see – 5 per page is what I want – but it also controls the number of posts displayed in the Dashboard as well. I want to be able to display more that 5 results per page when using the Dashboard for things like bulk editing.

    Is there a way to distinguish the number of pages displayed in these two areas in the code? Thanks!

    Here is part of the code from my custom plugin:

    add_action( 'pre_get_posts', 'porkopolis_pre_get_posts' );
    
    function porkopolis_pre_get_posts( $query ) {
    
    	if ( $query->is_main_query() && $query->is_tax( array( 'country', 'subject' ) ) ) {
    
    		// posts per page
    		$query->set( 'posts_per_page', 5 );
    
    		// ABC
    		$query->set( 'orderby', 'title' );
       		$query->set( 'order', 'ASC' );
    
    	}
    
    	elseif ( $query->is_main_query() && $query->is_post_type_archive( 'pig_poet' ) ) {
    
    		// posts per page
    		$query->set( 'posts_per_page', 5 );
    
    		// ABC
    		$query->set( 'orderby', 'title' );
       		$query->set( 'order', 'ASC' );
    	}
    	elseif ( $query->is_main_query() && $query->is_post_type_archive( 'pig_artist' ) ) {
    
    		// posts per page
    		$query->set( 'posts_per_page', 5 );
    
    		// ABC
    		$query->set( 'orderby', 'title' );
       		$query->set( 'order', 'ASC' );
    	}
    
    }
  2. Justin Tadlock

    After this line in your code:

    `
    function porkopolis_pre_get_posts( $query ) {
    `

    Add this:

    `
    if ( is_admin() )
    return;
    `

    Your screen options tab should work again after that.