WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#13020 closed defect (bug) (fixed)

WP_Query - Post type ignored in taxonomy queries.

Reported by: d910qf Owned by:
Milestone: 3.0 Priority: normal
Severity: normal Version: 3.0
Component: Posts, Post Types Keywords: has-patch, taxonomies, query
Focuses: Cc:

Description

Hi,

I posted this to the wp-hackers mailing list - when you specify a taxonomy term and post types using query_posts the post type is getting ignored on line 2034 of query.php ( $post_type = 'any'; ).

Not sure where to start with fixing it or if this intended?

[Edited rest of message]

This code however always ignores the post_type requirement but still
selects all post types linked to the term_name;

global $wp_query;
query_posts( array(
    'post_type' => 'my_post_type' ,
    "taxonomy_name" => "term_name",
    'showposts' => 10 )
);

Attachments (1)

13020.diff (586 bytes) - added by michaelh 5 years ago.
Otto identified what caused the problem. Dougal Campbell suggested the solution.

Download all attachments as: .zip

Change History (10)

comment:1 @michaelh5 years ago

  • Component changed from General to Post Types

@michaelh5 years ago

Otto identified what caused the problem. Dougal Campbell suggested the solution.

comment:2 @michaelh5 years ago

  • Keywords has-patch added

comment:3 @dd325 years ago

(In [14121]) Respect the post_type param for Taxonomy Queries, Props Otto & Dougal Campbell. Restrict Attachment post_status joins to queries involving attachment results ("any" or "attachment"). See #13020

comment:4 @dd325 years ago

  • Milestone changed from Unassigned to 3.0

Made a slight addition to the commit, The below SQL was added for all taxonomy queries, It'll not be included if a query is directly for a specific post_type (thats not an attachment).

LEFT JOIN wp_posts AS p2 ON (wp_posts.post_parent = p2.ID) .... ((wp_posts.post_status = 'publish') OR (wp_posts.post_status = 'inherit' AND (p2.post_status = 'publish')))

I've left this open for a question: the post_status will ALWAYS be overridden there still, I'm thinking that should be respected as well, If i request drafts in a taxonomy, i'd expect to recieve drafts.. This might be more important for those using custom post status's.

comment:5 @ryan5 years ago

Fixed?

comment:6 @michaelh5 years ago

With a post_type of 'book' and a custom taxonomy of 'genre', these query arguments work fine for me:

$args=array(
  'genre' => 'mystery',
  'post_type' => 'book',
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1
);
$my_query = null; 
$my_query = new WP_Query($args); 

and

$args=array(
  'genre' => 'mystery',
  'post_type' => 'book',
  'post_status' => 'draft',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1
);
$my_query = null; 
$my_query = new WP_Query($args); 

comment:7 @ryan5 years ago

  • Resolution set to fixed
  • Status changed from new to closed

comment:8 follow-up: @anointed5 years ago

I've tried every variation I can think of but all seem to return all post-types

example:

<?php
$wpq = array (post_type =>'movies','taxonomy'=>'series','term'=>$term->slug);
$query = new WP_Query ($wpq);
while ( have_posts()) : the_post(); ?>

<?php the_title(); ?>
do other stuff

<?php endwhile; ?>

Unless my function is wrong, I think it still may be broken

comment:9 in reply to: ↑ 8 @michaelh5 years ago

Replying to anointed:

I've tried every variation I can think of but all seem to return all post-types

example:

...

$wpq = array (post_type =>'movies','taxonomy'=>'series','term'=>$term->slug);

...

}}}

If your taxonomy is 'series' then

$wpq = array (post_type => 'movies', 'series' => $term->slug);
Note: See TracTickets for help on using tickets.