Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Some conditional logic for Query?

  • I’m trying to create a Related Posts grid that takes posts from an ACF field (so far so good) but which will fall back on showing randomly selected posts if no hand-curated ones are found.

    I hoped I could just put another Query inside the No Results block but WP definitely doesn’t like that 😅

    Is there another way?

    Grateful for any pointers!

  • Hi there,

    Are you using GB 1 or GB 2?

    If it’s the latter, you can add another query loop to the NO RESULTS container, it will show only if the main query has no result.y.

    If you are using GB 1, it will require a custom PHP code to add a fallback query.

  • Yeah that’s what I already tried (in GB2).

    When I nest a Query inside the No results container, WordPress starts giving errors (“There has been a critical error on this website.”).

    Bug?

  • I can not replicate the issue… it seems fine when I test.

    Check this screenshot: https://app.screencast.com/E7snYOqY2olbl

    Are you using GB 2.0 BETA 6?

    And can you try disabling all other plugins to eliminate plugin conflict?

  • Yes confirmed GB 2.0 beta 6 (both the pro and base) and the block structure is identical to your screenshot:
    https://imgur.com/a/HAkYP2G

    If it makes a difference:
    – I set the nested Query (within ‘no results’) to order by -> random
    – Everything is inside an Element using the generate_before_footer hook

    It seems that any page that doesn’t have data in the ACF field (i.e. which goes to the no results fallback) will work, but any page that has the ACF data will go to critical error.

    The initial Query is set to Post meta > Current post and uses the ACF field name as the meta key. When there’s nothing in the ‘no results’ block, it works.

  • I can replicate the issue with the same settings, I’ve forwarded it to the dev team, I’m still waiting for replies.

    For now, try adding the fallback query loop right after the main query block, and add a CSS class to the main query, eg. related-posts, add another CSS class to the fallback query, eg. fallback-posts.
    Adding CSS class(es): https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    Then add this CSS to hide the fallback posts when there’s no posts shown in the main query.

    .related-posts:has(.gb-loop-item) + .fallback-posts {
        display: none;
    }

    I’ll keep this topic open, once I hear from the dev team, I’ll post an update 🙂

  • So it is a bug, the dev team will try to fix it in the next release.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.