Multiple Post Type Queries
-
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-postI 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 278:
$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-postThen 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-postto 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.
The topic ‘Multiple Post Type Queries’ is closed to new replies.