[wp-trac] [WordPress Trac] #14330: Multiple Post Type in Page Query Problem

WordPress Trac wp-trac at lists.automattic.com
Fri Jul 16 18:07:44 UTC 2010


#14330: Multiple Post Type in Page Query Problem
--------------------------+-------------------------------------------------
 Reporter:  loushou       |       Owner:                                      
     Type:  defect (bug)  |      Status:  new                                 
 Priority:  normal        |   Milestone:  Awaiting Review                     
Component:  Query         |     Version:  3.0                                 
 Severity:  major         |    Keywords:  post_type, WP, WP_Query, classes.php
--------------------------+-------------------------------------------------
 PLEASE READ WHOLE BUG: because it includes reasons for severity also

 == THE PROBLEM: ==
 So I have a problem.... obviously, right? So if I were to do something
 like:

 {{{
 get_posts('post_type[]=post_type_1&post_type[]=post_type_2');
 }}}

 I would receive a list of posts that are either of post type 'post_type_1'
 or of 'post_type_2'. Now if I goto a url like the following:

 {{{
 http://www.my-
 site.com/index.php?post_type=post_type_1&post_type=post_type_2&name
 =testing-post
 }}}

 I should get a very very similar result, but instead, I get a 404 error
 page.

 How can I make the later example work? I have been browsing the WP base
 code (like always) and found the following line in classes.php on line
 277:

 {{{
 $this->query_vars[$wpvar] = (string) $this->query_vars[$wpvar];
 }}}

 Most probably don't know what this line actually means, but in short it
 converts all the query vars (everything after the ? in a url) to strings,
 even those that should be arrays (since the WP_Query object supports
 arrays). So instead my slightly parsed url winds up looking like:

 {{{
 http://www.my-site.com/index.php?post_type=Array&name=testing-post
 }}}

 because in PHP when performing a string type cast on an array, you get a
 string with the word 'Array':

 {{{
 $myArray = array(
   'some value',
   'another un-important value'
 );
 echo (string) $myArray;
 // prints out 'Array'
 }}}

 Then later in classes.php on line 289, we have this:

 {{{
 // Limit publicly queried post_types to those that are publicly_queryable
     if ( isset( $this->query_vars['post_type']) ) {
       $queryable_post_types =  get_post_types( array('publicly_queryable'
 => true) );
       if ( ! in_array( $this->query_vars['post_type'],
 $queryable_post_types ) )
         unset( $this->query_vars['post_type'] );
     }
 }}}

 which does what it says, eliminates my now post_type=Array from the query
 completely because 'Array' is not only not a proper post type, but it is
 not 'publicly_queryable'.

 So, again I ask, how do I get a url like:

 {{{
 http://www.my-
 site.com/index.php?post_type=post_type_1&post_type=post_type_2&name
 =testing-post
 }}}

 to work, assuming I do not know which post type it is?

 p.s. ~ for the really high-level programmers, it is actually a little more
 in depth than this, but this is the basic idea of the problem, and works
 exactly the same way as the actual problem. for a glimpse into the big
 picture problem, think about how WP interprets clean urls to determine the
 query used to pull the list of posts to display on the current page.


 == IMPORTANCE VALUE: ==

 This is marked 'major' because on of the key features of WP3.0 was better
 handling of custom post types. With this problem, all translations of the
 url where multiple post types are being queried will fail. Not only is
 this a post_type problem, but above should clearly explain that any query
 variable that the WP_Query class normally accepts in the form of an array,
 will be effected. This means that now translated urls like this:

 {{{
 http://www.my-
 site.com/index.php?post_status[]=publish&post_status[]=new_custom_status&post_status[]=inherit
 }}}

 will also not work.

 So because the scope of this problem directly conflicts with the awesome,
 much anticipated new features of WP, on a large scale, it is very
 important that it gets solved quickly.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/14330>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list