0

I have a custom post type with courses-events as the slug.

In functions.php, I have:

add_action( 'pre_get_posts', 'custom_archive_items' );
function custom_archive_items( $query ) {
  if ($query->is_main_query() && !is_admin() && is_post_type_archive( 'courses-events')) {
    $query->set( 'posts_per_page', '1' );
  }
}

I know the above is firing for my archive page if I dump $query in the condition, it is visible.

On archive-courses-events.php I have simply:

if ( have_posts() ) :
  while(have_posts()) : the_post();
    <!-- my output -->
  endwhile;
  
  echo paginate_links();
endif;

I have 2 posts and as posts_per_page is set to 1, but 2 posts appear and I do not see pagination.

I don't understand why this isn't working as everything I've read says that this is the minimum necessary for this to work?

2
  • Maybe there's a code (e.g. in a plugin) which is overriding your filter (or the posts_per_page setting), so you can try deactivating all plugins and see if the issue persists? Or try first with using a priority of 11 or greater, e.g. add_action( 'pre_get_posts', 'custom_archive_items', 20 );
    – Sally CJ
    Commented Jan 8, 2022 at 0:56
  • 1
    OH man, I feel a bit dumb but giving it a higher priority worked right away. Thank you. I completely overlooked that possibility.
    – arrr_matey
    Commented Jan 8, 2022 at 2:31

1 Answer 1

0

Answered in a comment:

try first with using a priority of 11 or greater, e.g.

add_action( 'pre_get_posts', 'custom_archive_items', 20 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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