WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#10605 closed defect (bug) (fixed)

WordPress Shouldn't Mess with post_type

Reported by: filosofo Owned by: ryan
Milestone: 2.9 Priority: normal
Severity: normal Version: 2.9
Component: Query Keywords: post_type, wp_query has-patch
Focuses: Cc:

Description

I have a custom post_type, "restaurant," and I'd like to query for the "restaurant" with name "joes-diner".

query_posts(array(
   'post_type' => 'restaurant',
   'name' => 'joes-diner',
));

fails because WP sees that I'm querying by name, sets $wp_query->is_single to true, which in turn sets post_type to "post."

If I wanted post_type to be "post" I wouldn't have specified "restaurant", would I?

Patch uses the given post_type when specified.

Attachments (4)

allow_custom_post_type.10605.diff (811 bytes) - added by filosofo 6 years ago.
allow_custom_post_type.10605.more_efficient.diff (812 bytes) - added by filosofo 6 years ago.
10605.diff (712 bytes) - added by dd32 6 years ago.
10605.2.diff (713 bytes) - added by dd32 6 years ago.

Download all attachments as: .zip

Change History (19)

comment:1 @filosofo6 years ago

  • Keywords has-patch added

comment:2 @dd326 years ago

attachment allow_custom_post_type.10605.diff added

I swear i've written that patch before.

Wouldnt it be better off written as:

{{{{
if ( 'any' == $post_type ) {
...
} elseif ( ! empty($post_type) ) {
$where .= " AND $wpdb->posts.post_type = '$post_type'";
} elseif ( is_page....
}}}

But I'm pretty sure it was written like that for a reason.. And i'm 100% certain theres other tickets for this

comment:3 @dd326 years ago

that code again..

if ( 'any' == $post_type ) {
   ...
} elseif ( ! empty($post_type) ) {
   $where .= " AND $wpdb->posts.post_type = '$post_type'"; 
} elseif ( is_page....

comment:4 @dd326 years ago

I may've been thinking of #8872 or #5740 But i know i've come across this before, and modified it in the way i mentioned. It may be that i never submitted a patch for it though.

comment:5 @filosofo6 years ago

Yours is more efficient. I've added it as a patch.

comment:6 @markjaquith6 years ago

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

(In [11817]) Always respect custom post_type in queries. props dd32, filosofo. fixes #10605

comment:7 @Lazy796 years ago

11817 - breaks my pages

site not found

sorry i am not familar with trac so i do not know if i should reopen it..

comment:8 @dd326 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

11817 - breaks my pages

Confirmed. I'm just going to work out whats causing it..

comment:9 @dd326 years ago

Its defaulting to 'post' which fufils the !empty() check.. See patch

I'm also a bit suspicious of the $post_type = 'any' on line 1963: http://core.trac.wordpress.org/browser/trunk/wp-includes/query.php#L1963 It'll prevent querying for any objects of type x which are in a taxonomy, ie. i ask for Posts assoc. with Term5 and i'll receive Posts and Pages assoc. with Term5.. (I think)

I've done some basic testing on this.. in that posts/pages/attachments are queried correctly.

@dd326 years ago

comment:10 @dd326 years ago

attachment 10605.diff added

  • Not sure what that !empty() change is for.. (Must've been there awhile then)
  • But that post -> empty string seems to do the trick

comment:11 @dd326 years ago

  • Version set to 2.9

@dd326 years ago

comment:12 @azaozz6 years ago

(In [11822]) Fix displaying pages and attachments, props dd32, see #10605

comment:13 @ryan6 years ago

(In [11960]) Set the cap appropriate to the post type. fixes #10807 see #10605

comment:14 @ryan6 years ago

Fixed?

comment:15 @ryan6 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.