1

I have the current function on my theme using pre_get_posts, currently right at the bottom of my theme's functions.php file:

function mytheme_portfolio_archive_pages( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_post_type_archive( 'mytheme_portfolio' ) ) {
        // Display 20 posts for a custom post type called 'mytheme_portfolio'
        $query->set( 'posts_per_page', 20 );
        return;
    }
}
add_action( 'pre_get_posts', 'mytheme_portfolio_archive_pages', 1 );

It means I have 20 posts per page if the current archive is of the post type mytheme_portfolio, instead of the default 5 for the blog pages (the number 5 is being set in "Blog pages show at most __ posts" in Settings → Reading).

The above function is working fine when Jetpack Infinite Scroll is deactivated.

Problem is I want to use Jetpack Infinite Scroll with the 'mytheme_portfolio' archive. And with it activated, the above function stops working and the Infinite Scroll uses the "Blog pages show at most 5 posts" number from Settings → Reading when it loads new posts. The Infinite Scroll is otherwise working fine but I need to show 20 posts per page, not 5, if I'm on a mytheme_portfolio archive page!

My Jetpack setup code is as follows, just for reference:

    function mytheme_render_infinite_scroll() {
        while ( have_posts() ) : the_post();
                if ('mytheme_portfolio' == get_post_type() && !is_search()) :
                    get_template_part( 'content', 'archive-portfolio' );
                elseif ( is_search() ) :
                    get_template_part('content','search');
                else :
                    get_template_part( 'content', get_post_format() );
                endif;
        endwhile;
    }

    function mytheme_jetpack_setup() {
        add_theme_support( 'infinite-scroll', array(
            'container'      => 'content',
            'footer_widgets' => 'footer-widgets',
            'type'           => 'click',
            'render'         => 'mytheme_render_infinite_scroll',
            'wrapper'        => true,
            'posts_per_page' => false
        ) );
    }
    add_action( 'after_setup_theme', 'mytheme_jetpack_setup' );

Thanks so much for any help anyone can give!

EDIT: I found the line in the Jetpack plugin code that's conflicting, in jetpack/modules/infinite-scroll/infinity.php, line 26...

function __construct() {
    add_action( 'pre_get_posts',                  array( $this, 'posts_per_page_query' ) );
    ...

which corresponds to lines 449 - 452 in the same file:

function posts_per_page_query( $query ) {
    if ( ! is_admin() && self::archive_supports_infinity() && $query->is_main_query() )
        $query->set( 'posts_per_page', self::get_settings()->posts_per_page );
}

If I comment line 26 out my function works. Obviously I don't want to go changing the plugin's files, is there a way I can make sure my theme's posts_per_page argument (initial code block above) is the one that's used, and not Jetpack's?

  • Isn't this the same question as here: wordpress.stackexchange.com/q/127163/21376 ? – s_ha_dum Dec 26 '13 at 17:26
  • Hi, that's my question too, it is related but I thought this could be seen as a separate issue - in the question you linked to Jetpack IS's built in posts_per_page argument is not having any effect, but in the above question Jetpack IS is conflicting with a completely separate/external to Jetpack function. With both questions I want to achieve the same goal - more posts showing per page on the portfolio pages than the default, but I'm trying two different methods. Thought someone may have a contribution to this method as I haven't received any answers on the previous question. – Sarah Dec 26 '13 at 17:31
3

You've specified a priority of 1 for your pre_get_posts function here:

add_action( 'pre_get_posts', 'mytheme_portfolio_archive_pages', 1 );

Jetpack doesn't specify a priority:

add_action( 'pre_get_posts', array( $this, 'posts_per_page_query' ) );

So that gets added with the default priority for add_action, which is 10:

$priority (int) (optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. Default: 10

So for each of these requests, your code is executed and sets posts per page to 20, then Jetpack's code is executed and overwrites your value with its own.

To fix this, just specify a lower priority so yours runs later and overwrites Jetpack's:

add_action( 'pre_get_posts', 'mytheme_portfolio_archive_pages', 20 );
  • Thank you thank you thank you! Ironically I tried setting my priority to 10 as I thought that might be it, I didn't realise that the default was 10 too! I've set it higher now and it works a charm :) Thanks so much! – Sarah Dec 26 '13 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.