[wp-trac] [WordPress Trac] #48047: Querying (non-CPT) posts from more than one (native) category only returning posts from first category

WordPress Trac noreply at wordpress.org
Thu Sep 11 14:13:48 UTC 2025


#48047: Querying (non-CPT) posts from more than one (native) category only
returning posts from first category
-------------------------------+------------------------------
 Reporter:  lialyn             |       Owner:  (none)
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Query              |     Version:  5.2.3
 Severity:  normal             |  Resolution:
 Keywords:  2nd-opinion close  |     Focuses:  template
-------------------------------+------------------------------
Changes (by mindctrl):

 * keywords:   => 2nd-opinion close


Comment:

 Hi @lialyn, thanks for the report. I believe this to be intentional, due
 to the code you showed from `get_posts()` internals. I think the suggested
 way to "get" these posts, is to loop through using the methods like this
 example:

 {{{
 $args = array(
         'post_type' => 'post',
         'posts_per_page' => 30,
         'post_status' => 'publish',
         'category__in' => [4,5],
 );

 $query = new WP_Query($args);

 if ( $query->have_posts() ) {
         while ( $query->have_posts() ) {
                 $query->the_post();
                 // Output your post content here
                 the_title( '<h2>', '</h2>' );
                 the_content();
         }
         wp_reset_postdata();
 } else {
         echo 'No posts found.';
 }
 }}}

 You can also access them the way you mentioned, via the public
 `$query->posts` property.

 Another way is to use the global `get_posts()` function, which should give
 you the full array of WP_Post objects:
 {{{
 $posts = get_posts( $args );
 }}}

 I don't think there's a bug here we can fix, due to backwards
 compatibility, but curious to hear other opinions.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/48047#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list