I’ve got a big query that queries one post_type with all different kinds of filters. Values are taken from <form> inputs.

  • Im adding 3 different checkboxes to the <form> which can
    be used as post_type toggle – this means that you can query only one post_type of the three, two of them or all at once
  • Every post_type has it’s own inputs that I show / hide with jQuery
  • post_type inputs should only affect / filter its own post_type
  • All the results must be displayed in same list

My question is: how to query different post_types when each have its own arguments?


Thoughts after few days of thinking about this:

  • Three different queries (poor, poor performance) and compile multiple queries into one loop?

    • While Im having tons of query arguments, it should drastically speed up query because there’s pretty narrow scope, at least my logic says it and three different queries shouldn’t be that bad even if each have thousands of posts?

    • My only concern is that user can also query without filling any inputs which means that all three queries are quering ALL the posts.

  • Including several post_types seems impossible – you cannot use different metas and taxes on different post_types using conditional logic in same query, right?

1 Answer
1

That would be nice if you query all three post_types at the same time, using one query, and then manipulating it using php.

Consider this query:

$args = array(
    'post_type' => array( 'pt1', 'pt2', 'pt3' ),
    'meta_key' => BUMP!!!!
);

You can not use an array of meta_keys.

So, I think the only way is to use wp_db object and make your own custom query:

$results = $wpdb->get_results( 
    "
    SELECT ID, post_title 
    FROM $wpdb->posts
    WHERE post_type="pt1" OR post_type="pt2" OR post_type="pt3" 
        AND ...
    "
);

foreach( $results as $result ){
    echo $result->post_title;
    // ...
}

and then use php to separate and show the results.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *