Title: Query loop can&#8217;t display multiple post types
Last modified: November 28, 2024

---

# Query loop can’t display multiple post types

 *  [danidp89](https://wordpress.org/support/users/danidp89/)
 * (@danidp89)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/query-loop-cant-display-multiple-post-types/)
 * I have four custom post types, and am using the following code snippet (using
   WPCode) to display them all at the same time in one query loop:
 *     ```wp-block-code
       function filter_specific_query_loop($query) {    // Check if this is an admin query or not the main query    if (is_admin() || !$query->is_main_query()) {        return;    }    // Check for the presence of the custom query parameter    if (isset($query->query_vars['custom_query_loop']) && $query->query_vars['custom_query_loop']) {        // Modify the query to include multiple post types        $query->set('post_type', array('post', 'restaurants', 'activities', 'equipment','recipes'));    }}add_action('pre_get_posts', 'filter_specific_query_loop');// Add a filter to include the custom query parameter when rendering the blockfunction add_custom_query_var($block_content, $block) {    if ($block['blockName'] === 'core/query') {        if (isset($block['attrs']['className']) && strpos($block['attrs']['className'], 'custom-query-loop') !== false) {            add_filter('query_vars', function ($query_vars) {                $query_vars[] = 'custom_query_loop';                return $query_vars;            });        }    }    return $block_content;}add_filter('render_block', 'add_custom_query_var', 10, 2);
       ```
   
 * I added custom class name: custom-query-loop to the query loop (located on home
   page). When I preview my page however, I only see one post type from the query
   loop.
 * Am I doing something wrong? My site is not live yet.
 * Thank you in advance for help!

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/query-loop-cant-display-multiple-post-types/#post-18167970)
 * Query loop queries are generally not main queries, so `|| !$query->is_main_query()`
   is preventing your code from modifying the query. The actual main query is for
   the page or post the query loop is on. Remove the `!` not operator so your code
   can alter the query loop query.
 *  Thread Starter [danidp89](https://wordpress.org/support/users/danidp89/)
 * (@danidp89)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/query-loop-cant-display-multiple-post-types/#post-18169486)
 * Thanks for the advice! I tried removing the `!` not operator, as well just deleting`
   || !$query->is_main_query()`, but still only see one post type when previewing
   my page in either case. Do you have any other thoughts?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/query-loop-cant-display-multiple-post-types/#post-18169950)
 * Nothing more than basic debugging really. Verify the query var “custom_query_loop”
   that you’re checking for is really set. But first be sure debug output is visible
   on the page. If not you may need to use `error_log()`.
 * If the query var is not set, there could be an issue with add_custom_query_var()
   and not filter_specific_query_loop() where my attention has been focused.
 * Maybe it’s possible “pre_get_posts” fires before “render_block”, so your method
   of identifying the right query to modify might be flawed? (unverified)
 * Check the query object’s `request` property, the actual SQL query. There could
   be some other constraint that’s preventing non-post types from being returned.
 *  Anonymous User
 * (@anonymized-23157725)
 * [1 year, 2 months ago](https://wordpress.org/support/topic/query-loop-cant-display-multiple-post-types/#post-18360955)
 * add custom post type vars to your query code since it does not recognize as a
   post.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Query loop can’t display multiple post types’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [query loop](https://wordpress.org/support/topic-tag/query-loop/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 3 participants
 * Last reply from: Anonymous User
 * Last activity: [1 year, 2 months ago](https://wordpress.org/support/topic/query-loop-cant-display-multiple-post-types/#post-18360955)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
