Archive page for Wordpress Custom Post Type doesn't show pagination from paginate_links()

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?

Topic paginate-links custom-post-type-archives php custom-post-types Wordpress

Category Web


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 );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.