WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

#33318 closed defect (bug) (invalid)

tax_query breaks certain other wp_query args

Reported by: djripraw Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.2.4
Component: Query Keywords: reporter-feedback
Focuses: template Cc:

Description

I'm finding on a single post page that creating a wp_query with tax_query included in the args breaks certain other query arguments. Namely the posts_per_page argument isn't working. If i remove the tax_query, posts_per_page works.

<?php
$args_new = array(
	'post_type' => 'store',
	'posts_per_page' => 2,
	'orderby' => 'date',
	'order' => 'DESC',
	'tax_query' => array(
		array(
			'taxonomy' => 'store_artist',
			'field'    => 'term_id',
			'terms'    => array( $artist_store_link ),
		),
	),
);
$query2 = new WP_Query( $args_new );
?>

<?php if ( $query2->have_posts() ) : ?>

<div class="cell-12 cell-lg-12">
	
	<h2 class="headline-2">Tracks by <?php echo get_the_title(); ?></h2>
	<div class="group">

	<?php while ( $query2->have_posts() ) : $query2->the_post(); ?>
		
		<?php get_template_part('partials/store-archive'); ?>
		
	<?php endwhile; ?>

	<?php wp_reset_postdata(); ?>

	</div>
</div>
	
<?php endif; ?>

Attachments (1)

33318.diff (1.6 KB) - added by boonebgorges 4 years ago.

Download all attachments as: .zip

Change History (10)

#1 @boonebgorges
5 years ago

  • Keywords reporter-feedback added

Hi djripraw - Thanks for the ticket, and welcome to WordPress Trac.

When you say that 'posts_per_page' is "broken" in this case, what do you mean? Are you getting back *fewer* than 2 posts, or *more* than 2? If the answer is "fewer", perhaps it's because there are fewer than 2 posts that meet the criteria in your 'tax_query'?

Can you give more details on how to reproduce on a vanilla WP installation?

#2 @m_i_n
4 years ago

I've experienced the same issue. The answer to your question is - all posts are returned (looks like posts_per_page == -1).
Tested on WP 4.5-RC1.

This ticket was mentioned in Slack in #core by boone. View the logs.


4 years ago

#4 @boonebgorges
4 years ago

@m_i_n or anyone else having the issue - Can you give more details? tax_query shouldn't touch pagination in WP_Query, so we wonder whether there may be an external actor here - like maybe a pre_get_posts filter that's interfering with pagination in the presence of tax_query.

#5 @m_i_n
4 years ago

I've made some tests and I can see it matters if it's a custom post type, not regular post.

This query gives 2 results...

<?php
$args = array(
	'post_type'      => 'books',
	'posts_per_page' => 2
);
$query = new WP_Query($args);

but...

<?php
$args = array(
	'post_type'      => 'books',
	'posts_per_page' => 2,
	'tax_query'      => array(
		array(
			'taxonomy' => 'book-category',
			'field'    => 'term_id',
			'terms'    => array(16),
		)
	)
);
$query = new WP_Query($args);

gives all book-category=16 results like the posts_per_page parameter would not exist.

#6 @boonebgorges
4 years ago

@m_i_n Thanks for the additional info. I'm still unable to reproduce. See the test in 33318.diff (which passes).

Is this happening for *any* post type, or just for the 'books' type? I have a hunch that something else in your plugin - the one that registers 'books' - is hooking into the query and modifying it in a way that's causing the error. Is it a plugin that you wrote yourself? Can you share it?

@boonebgorges
4 years ago

#7 @m_i_n
4 years ago

My previous code was example, but now i delete as much as i can unnecessary code from my real source and at the end i have this:

register_post_type('portfolio', array(
	'public'       => true,
	'hierarchical' => true
));
register_taxonomy('portfolio-category', 'portfolio', array(
	'hierarchical' => true
));
$query = new \WP_Query(array(
	'posts_per_page'  => 2,
	'post_type'       => 'portfolio',
	'tax_query'       => array(
		array(
			'taxonomy' => 'portfolio-category',
			'field'    => 'term_id',
			'terms'    => array(222)
		)
	)
));
var_dump(count($query->posts));

Result: 5 (should be 2).

public and hierarchical doesn't change anything.

When i delete tax_query: 2.
When i change terms in tax_query for non-existing term: 0.
When i change post_type to post (and properly adapt tax_query): 2.

I'm trying to run query in shortcode, in content, before header, after footer, and it also doesn't have matter.

Sorry for my english ;)

#8 @m_i_n
4 years ago

Ok, after some more deep digging into code, i have that action:

public function actionPreGetPosts($query)
{
	if ($query->is_tax('portfolio-category') || $query->is_tax('portfolio-tag')) {
		$query->query_vars['posts_per_page'] = self::to('portfolio/archive/count');
	}
}

...which refers to archive page, but it also destroy custom WP_Query call.

Sorry for a trouble (very big sorry), problem solved.

Last edited 4 years ago by m_i_n (previous) (diff)

#9 @boonebgorges
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Sorry for a trouble (very big sorry), problem solved.

No problem. Glad it wasn't a WordPress bug :)

Note: See TracTickets for help on using tickets.