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.

How to create a Query Loop for custom pages

  • Hi,
    I want to create 7 similar pages with the same look and feel using a generate block element.
    the pages will be like a “category” page but totally custom with some post.
    I am good creating those individually but I wonder if it was possible to create a block hook element that will appear on those 7 pages but making the query independent for them.
    So on the page I would like to edit the query and the template block will capture it and show the different post for each page.

    So far, because they are pages, the main query loop is empty on those, so I cannot use that (or I do not know how to do that).
    Is there any way to have a main loop on a block inside the page and then capture that with the block hook and use it for the shared template?

    Let me know if you need more information, it is hard to explain. 🙂

    Thanks

  • Hi there,

    You can use ACF plugin to create a relationship field for the pages, and in the page editor, you can select the posts you want to show in the query loop.

    As GB does not have support for relationship field yet, you will need to write a bit of PHP code to alter the query loop block’s output. Try follow the steps below:

    1. Add a CSS class to the grid block of the query loop, eg. highlight-posts.

    2. Add this PHP code, change acf_highlight_posts to the actual ACF field slug.

    //set post type and post per page in the query loop block
    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        // find the block with <code>my-class-name</code>
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'highlight-posts' ) !== false &&
            ! is_admin()
        ) {
            // get the related post ID
            $queried_object = get_queried_object () ;
            $relationship = get_field( "acf_highlight_posts", $queried_object );
          
          // Merge the current $query_args with the new args
            return array_merge( $query_args, array(
                'post__in' => $relationship,
            ) );
        }
    
        return $query_args;
    }, 10, 2 );

    Let me know if this helps!

  • Thank you for the great reply!

    I was trying to avoid coding on this project as I want it to be low maintenance in the future.
    I think because they are 7 pages, I can do this manually page by page at the end.

    But I will give it a try!

    Kind regards!

  • No Problem 🙂

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