Support

Account

Home Forums General Issues Multiple/nested query in a single loop

Solving

Multiple/nested query in a single loop

  • I have a question concerning multiple or nested queries in a single loop and I’ve tried several solutions but none of them worked. I think I haven’t really understood the WP_Query so any help is very welcome.

    The situation: I have posts about video numbers with 4 different fields, eg.:

    Video # Topic Time Check (Y/N)

    1 Some text 01:45 No
    1 03:50 Yes
    1 05:20 No
    2 Other text 02:30 No
    2 04:20 No
    3 More text 03:20 No
    3 05:30 Yes
    … and so on

    What I need is a list with:
    – the video number
    – the topic of the first post of any video
    – the time of the last post
    – if any of the videos have a YES as checked.

    In the above example the list would look like:

    1 Some text 05:20 Yes
    2 Other text 04:20 No
    3 More text 05:30 Yes

    My problem is that I even don’t have a clue about which way to solve this issue would be best or easiest. As I said, any help is very welcome!

    Thank you and greetings!
    Adriano

  • Hi @sonnenschauer

    If you use a repeater, then please check this article about nested repeater: https://www.advancedcustomfields.com/resources/working-with-nested-repeaters/.

    If you have read that article but still have the issue, could you please share the JSON or XML export file of your field group and the code you’ve tried? You can paste the PHP template code here: https://gist.github.com/.

    Also, could you please share some screenshots of the issue with a note on how you want it to be?

    Thanks 🙂

  • Thanks for your response!

    No, I don’t use repeaters. You’ll find the actual code here: https://gist.github.com/anonymous/944264e6cb6f10ea4948ef4240cf2b60

    Actually I get a list of all entries with their field values. Thats not bad, but I want to merge every video with specific field entries:
    – Video #
    – Topic (topic-field in first scene for every video)
    – Time (time-field in last scene for every video)
    – Check (if any on the scene for every video is checked)

    I’ve made a screenshot with the actual list and two examples of merged entries on video 1 and 5.

    Multiple Queries

    Hope this helps. Thanks for your help!

  • Hi @sonnenschauer

    I believe you posted the scripts as separate posts with the same post title. I’m afraid this is more of a PHP and WP issue than ACF issue. For something like this, I suggest you ask to PHP or WP community instead. But, I have created an example logic for this situation:

    <?php
    if ( $custom_query->have_posts() ) :
    
        $current_video_number = false;
        $current_topic = false;
        $current_timestamp = false;
        $current_chosen = false;
        
        while ( $custom_query->have_posts() ) : $custom_query->the_post();
        
            $temp_video_number = get_field('video_number');
            $temp_topic = get_field('topic');
            $temp_timestamp = get_field('timestamp');
            $temp_chosen = get_field('chosen');
            
            if( !$current_video_number ){
                $current_video_number = $temp_video_number;
                $current_topic = $temp_topic;
                $current_timestamp = $temp_timestamp;
                $current_chosen = $temp_chosen;
            } elseif ( $current_video_number == $temp_video_number ) {
                $current_timestamp = $temp_timestamp;
                if( $temp_chosen == 'Yes' || ($current_chosen == 'No' && $temp_chosen == 'Yes') ) {
                    $current_chosen = 'Yes';
                } else {
                    $current_chosen = 'No';
                }
            } else {
                echo $current_video_number;
                echo $current_topic;
                echo $current_timestamp;
                echo $current_chosen;
                
                $current_video_number = $temp_video_number;
                $current_topic = $temp_topic;
                $current_timestamp = $temp_timestamp;
                $current_chosen = $temp_chosen;
            }
    
        endwhile;
        
    endif;
    wp_reset_postdata(); ?>

    If you need further support, please consult with PHP or WordPress community instead.

    Thanks 🙂

  • Ok, thanks for your feedback! In the meantime I think I fixed the problem by myself by creating indiviual queries for every entry, all surrounded by a variable for every video thats gives me the video number and which I use as meta_value.

    Thanks for your help anyway and greetings!
    Adriano

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Multiple/nested query in a single loop’ is closed to new replies.