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.

Trouble with a Filtered Query Loop

  • I’m having some trouble I can’t figure out. I created a query loop that filters content based on the presence of a custom field. It’s also set to only show 3 posts. However, sometimes it works perfectly (shows 3 posts, images all formatted properly), but then other times it only shows 2, and then other times it doesn’t format the photo properly. The inconsistency of the behavior is confusing me.

    Based on other threads in the support area, I am filtering the records by creating a custom snippet (pasted below) and assigning a class to the grid block of the query loop.

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    
        if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'am-to-sell' ) !== false ) {
    
            $meta_query = array(
                array(
                    'key' => 'inventory_status',
                    'value' => 'For Sale', // 0 is 'To Sell' and 2 is 'Sold'
                    'compare' => '=', // Use '=' for true, '!=' for false
                ),
            );
    
            $query_args = array_merge( $query_args, array(
                'meta_query' => $meta_query,
            ) );
        }
    
        return $query_args;
    }, 10, 2 );

    Screenshots: https://postimg.cc/gallery/xd6wZ78

    Thanks for any assistance you might be able to offer.

    — Aaron

  • Hi Aaron,

    The code looks correct, I wonder if it’s the random order that is having issues.

    Can you try to remove the random order for the query loop to test?

    Let me know 🙂

  • That seems to have fixed the issue, but I do need it to be random so it always displays three random cars. Any thoughts on how to fix that? Thanks.

    — Aaron

  • When it comes to querying a database, the Random parameter can be one of the most expensive queries to run. And it is for that reason that some hosts disable it by default on their servers.

    Another reason that Random may not actually work is caching. For best performance pages are often cached. Which may include the random list of posts that were generated on first page visit.

    So if you do run into an issue where Order By -> Random doesn’t seem to be working, it may be best to check your server and cache settings.

    I would recommend adding a view more button to redirect users to the actual archive instead of using random order.

  • I already do have a View More button in there. I was just hoping to have the cars be a bit dynamic in case different cars catch different people’s attention. But, if the query really is that taxing then perhaps it’s not worth it. I’ll just go with the button and no randomization.

  • Good choice 🙂

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