1

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();
?>
2
  • 1
    Your query looks correct, assuming that a) your posts are published, and b) you're certain that both is_archive() and is_search() return false for this page. The setting you refer to will not override posts_per_page by default. You're certain you have no pre_get_posts filters modifying the query?
    – vancoder
    Commented Feb 25, 2022 at 21:05
  • 1
    Incidentally, published is not a valid status, it should be publish, but that wouldn't cause your issue.
    – vancoder
    Commented Feb 25, 2022 at 21:14

0

Your Answer

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

Browse other questions tagged or ask your own question.