Make WordPress Core

Opened 15 years ago

Closed 14 years ago

#10605 closed defect (bug) (fixed)

WordPress Shouldn't Mess with post_type

Reported by: filosofo's profile filosofo Owned by: ryan's profile 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 15 years ago.
allow_custom_post_type.10605.more_efficient.diff (812 bytes) - added by filosofo 15 years ago.
10605.diff (712 bytes) - added by dd32 15 years ago.
10605.2.diff (713 bytes) - added by dd32 15 years ago.

Download all attachments as: .zip

Change History (19)

#1 @filosofo
15 years ago

  • Keywords has-patch added

#2 @dd32
15 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

#3 @dd32
15 years ago

that code again..

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

#4 @dd32
15 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.

#5 @filosofo
15 years ago

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

#6 @markjaquith
15 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

#7 @Lazy79
15 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..

#8 @dd32
15 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..

#9 @dd32
15 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.

@dd32
15 years ago

#10 @dd32
15 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

#11 @dd32
15 years ago

  • Version set to 2.9

@dd32
15 years ago

#12 @azaozz
15 years ago

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

#13 @ryan
15 years ago

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

#14 @ryan
15 years ago

Fixed?

#15 @ryan
14 years ago

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