• 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:

    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 block
    function 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

    (@bcworkz)

    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

    (@danidp89)

    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

    (@bcworkz)

    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)

    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.