WordPress.org

Ready to get started?Download WordPress

Forums

cbnet Different Posts Per Page
[resolved] Custom post types (4 posts)

  1. Martijnchel
    Member
    Posted 1 year ago #

    Hi,

    I managed to get the latest version of the plugin working on my websites (updated from the old version) and everything works fine. There is only one problem; i have some custom post types which need their own number of posts per page..

    In the old plugin I could add these, now I don't see them anymore. For me this is a big problem so is there a way to add custom post types?

    http://wordpress.org/extend/plugins/cbnet-different-posts-per-page/

  2. Chip Bennett
    Theme Review Admin
    Plugin Author

    Posted 1 year ago #

    Hi Martjinchel,

    Unfortunately, one of the (perhaps temporary) casualties of completely rewriting the Plugin was removing the ability to add arbitrary categories/post types/etc. I am exploring ways to re-implement that functionality, and if I find something I like, I'll be sure to add it in again.

    In the meantime, you can add it yourself, fairly simply, with the following hook/callback (which should be placed in functions.php or in a site-specific Plugin). Assuming you have:

    CPT: 'foo'
    Posts per page: 5
    Order: ascending

    CPT: 'bar'
    Posts per page: 3
    Order: descending

    function martjinchel_filter_pre_get_posts( $query ) {
        if ( is_post_type_archive( 'foo' ) && $query->is_main_query() ) {
            $query->set( 'posts_per_page', '5' );
            $query->set( 'order', 'ASC' );
        }
        if ( is_post_type_archive( 'bar' ) && $query->is_main_query() ) {
            $query->set( 'posts_per_page', '3' );
            $query->set( 'order', 'DESC' );
        }
    }
    add_action( 'pre_get_posts', 'martjinchel_filter_pre_get_posts', 11 );

    This is, in fact, all the Plugin does now. Just wash, rinse, and repeat for each custom post type you need to modify. By adding priority 11, you ensure your code executes after the Plugin, so your query filters will take precedence.

  3. Martijnchel
    Member
    Posted 1 year ago #

    Hi Chip,

    Thanks! Will try that first thing tomorrow.
    One question though; what do I replace the part where my name is with? I don't know much about this, so that would help :)

    Thanks

  4. Chip Bennett
    Theme Review Admin
    Plugin Author

    Posted 1 year ago #

    One question though; what do I replace the part where my name is with?

    You don't have to change that. You can use it as-is. :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.