wp query – posts_per_page in custom WP_Query does not override “Reading” settings?

Question

I am trying to use WP_Query loop (below) to show all instances of a certain post type.

(This loop is not in the custom post type’s archive — it is in a template part that appears as a page section on two different pages of the site.)

I have posts_per_page set in the WP_query args, but the argument is ignored and the number of posts displayed is instead whatever number of blog posts is set in the WordPress “Reading” settings.

My (two-part) question is:

  1. How do I display all of the posts in this custom post type loop (while leaving the blog archive (index.php) set to display just 9 posts at a time)?
  2. What is the point of the posts_per_page argument existing and being available to set on individual queries, if it is overridden by the global setting in the dashboard GUI?? What does this argument even do if it can’t be used to set the number of posts displayed by an individual query?

Here is the code I am using for my query/loop:

<?php

$args = array(
  'posts_per_page' => -1, /* have also tried setting to 999 or using a variable -- is still ignored */
  'post_type' => 'department',
  'status'    => 'published'
);

$serviceIcons = new WP_Query($args);

if ($serviceIcons->have_posts() ) : 
  // Loop department posts
  while ($serviceIcons->have_posts() ) : $serviceIcons->the_post();

    get_template_part( 'template-parts/icon-grid-item' );

  endwhile;
          
endif;

wp_reset_postdata();
?>

0
dubyaD 6 months 2022-02-25T15:28:32-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse