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.

Query Loop on self with filters

  • Hi I have a question on making a Query Loop, where I need multiple levels on the same tabel to show.

    Example:
    CPT 1 – contains people
    CPT 2 – contains live events of people and also relations or tags
    Imagine CPT 2 containing fields like:
    Person | Live event | taxonomy: place 1, place 2, place 3, monday, tuesday
    – Line 1 | Person 1 | being born | place 1
    – Line 2 | Person 1 | being born | monday
    – Line 3 | Person 1 | growing big | place 2

    What I want to setup:
    Page Person 1
    Live Events Query
    – Event 1 – being born
    – in place 1
    – on monday
    – Event 2 – growing big
    – in place 2

    So basically I would query CPT 2. But how can I make this “nesting”? Where in this case the Event is found 2 times, but I want to show it 1 time and then as details 2 times inside. I tried a Query Loop inside of a Query loop but I don’t have the filter to filter on a field to show it once.

    For now I used a Custom Taxonomy to reference CPT 1 en CPT 2 with a Person > Live event structure.

    do you have a direction?

  • Hi there,

    the Live Events, is each Event a separate post ? Or is it using something like a repeater field

  • Yes, the image below shows how it’s setup:

    2024-09-27-13-01-48

    If it works better with relational fields that can be done too. But I have the idea that having many ACF relation fields makes the site slow.

  • So it requires some relationship between the Person Post and the Events CPT.
    Then hook a Query Loop into your single person template. The Query Loop should simply be set to show all the Events posttype ( no other parameters ) and should include all the relevant dynamic data.

    The next step is to get the “relationship” value and using the generateblocks_query_loop_args hook to pass that into the query parameters.

    Here is an example PHP Snippet for that:

    
    
    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        // Find the block with the class 'event-posts'
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'event-posts' ) !== false &&
            ! is_admin()
        ) {
            // Get the current queried object
            $queried_object = get_queried_object();
            
            // Get the ACF relationship field value
            $relationship = get_field( 'your_acf_relation_field, $queried_object );
            
            // If there are related posts, modify the query arguments
            if ( $relationship ) {
                $query_args = array_merge( $query_args, array(
                    'post__in' => $relationship,
                ) );
            } else {
                // Return an empty query if no relationship is found
                $query_args = array_merge( $query_args, array(
                    'post__in' => [0], // No posts will be shown
                ) );
            }
        }
    
        // Return the query args, modified or not
        return $query_args;
    
    }, 10, 2 );
    

    This examples works with ACF relationship fields.
    In the code:

    1. the class name
    strpos( $attributes['className'], 'event-posts' ) !== false &&
    we set class name of event-posts that needs to be aded to the Grid Block inside the Query Loop.

    2. relationship:
    $relationship = get_field( 'your_acf_relation_field, $queried_object );

    here you would replace the your_acf_relation_field with your ACF field.

    You can of course replace the ACF relationship with some other relation , as long as it can be used to query the posts it should work.

  • Thank you, I will try this out.

  • Let us know how you get on !

  • I think it’s a start.

    However it now shows just all the events like:

    – lineage of
    – mariage of
    – mariage of
    – death of
    – death of

    Listing them. But what I want is a structure of the live events, where the live event itself only shows 1 time and then inside shows the amount of live events with the same name in the quantity that there are those events.
    – mariage of
    — mariage of 1
    — mariage of 2
    – death of
    — death of 1
    — death of 2

    Where I can replace the 2nd level fields with detail fields or related content to the Event CPT. I tried to do this with a Query loop inside the first Query loop. That would work if I could filter the top level query loop to show an event once. But is probably slower.

  • Hmmm… I am not sure on this.
    Have you looked at ACF repeated fields as those allow you to add multiple entries to a single post.
    It would need some custom development to return those fields, but I think it may make more sense data structure wise…

  • Hi,

    Thanks for the respons and feedback. I had not looked into the ACF repeated fields, now I did. Looks nice, however that also looks like using both GenerateBlocks and ACF for the same purpose and different styling.

    Your feedback on data structure makes sense. In this situation it would be logic to have a ‘table’ in between the entity – events – events details –> related.

    One concern on the data structure is that I get so many CPT’s of which some are for the single purpose relating. I had is that the Query loop only works on posts and not on taxonomies. If I could use the Query loop on taxonomies I could use those as registers. Would that be an option somehow?

  • No, the query loop does not work for taxonomies, there’s no way to do so, unfortunately.

  • Ok thanks.

  • No Problem 🙂

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