Support

Account

Home Forums Add-ons Repeater Field Acf while loop if any rows return this value once?

Solved

Acf while loop if any rows return this value once?

  • Hi,

    I’m building this thing where the administrator is uploading files to a page with a repeater field. Each row or file has a restriction to a user role via a select field.

    This works. But outside the repeater, there is a field for a headline above the downloadable files.

    Let’s say the headline says: Instructions and to the instructions, there are 10 files. If i’m a user without permission to see any of those files I would like to hide the headline outside the loop.

    Is there any way I can check the loop if the current user role matches any of the files and if not hide the headline outside the loop?

    This is how my loop looks:

    echo '<h3>' . $download_headline . '</h3>';
    
            while ( have_rows('section_download', 'options_customer') ) : the_row();
    
                //File restricted to
                $file_restriction  = get_sub_field('role_restriction');
                //Current user group
                $wcb2b_current_user_group = get_the_author_meta( 'wcb2b_group', $current_user->ID );
                
                if( in_array($wcb2b_current_user_group, $file_restriction ) ) { 
    
                }
    
            endwhile;
  • I assume you would be logged in to view this, allowing the correct user role to be allocated.

    Therefore, why don’t you check the user role outside of the repeater?

    Something like:

    <?php
    global $post;
    $author_id = $post->post_author;
    
    $user = get_userdata( $author_id );
    
    // Get all the user roles as an array.
    $user_roles = $user->roles;
    
    $author_roles = array('administrator', 'editor', 'contributor');
    
    if( array_intersect($author_roles, $user->roles ) ) :
    	echo '<h3>' . $download_headline . '</h3>';
    endif;
    
    while ( have_rows('section_download', 'options_customer') ) : the_row();
    
    	//File restricted to
    	$file_restriction  = get_sub_field('role_restriction');
    	//Current user group
    	$wcb2b_current_user_group = get_the_author_meta( 'wcb2b_group', $current_user->ID );
    
    	if( in_array($wcb2b_current_user_group, $file_restriction ) ) { 
    
    	}
    
    endwhile;
  • That’s not the problem or the question. The problem is that I want to check my loop for files that are restricted to some user roles. If there aren’t any files to my user role, I shouldn’t see the headline. I can still see other files and access the page.

  • You asked:

    I would like to hide the headline outside the loop

    I assumed $download_headline was therefore outputting the headline.

    As such, the code solution I gave did exactly that!

    Given the code provided, this gets the user role of the logged in person – this much I believe would be required to ensure your initial code works.

    You can therefore wrap your file inside the loop with the same conditional:

    if( array_intersect($author_roles, $user->roles ) ) :
    	//YOUR FILE
    endif;
  • I understand what you are saying. But the headline should not be visible depending on the user role alone, but insted if there are any files in the loop connected to the user role. You understand?

  • If I’m able to view 5 files depending on my user role below the headline “Instructions” I will see that specific headline. If I’m not allowed to see any of the instructions-files depending on my user role, I shouldn’t see the headline either.

  • The below should achieve just that:

    <?php
    global $post;
    $author_id = $post->post_author;
    
    $user = get_userdata( $author_id );
    
    // Get all the user roles as an array.
    $user_roles = $user->roles;
    
    $author_roles = array('administrator', 'editor', 'contributor');
    
    if( have_rows('section_download', 'options_customer') ):
    
    	if( array_intersect($author_roles, $user->roles ) ) :
    		echo '<h3>' . $download_headline . '</h3>';
    	endif;
    	echo '<ul>';
    	while ( have_rows('section_download', 'options_customer') ) : the_row();
    
    		//File restricted to
    		$file_restriction  = get_sub_field('role_restriction');
    		//Current user group
    		$wcb2b_current_user_group = get_the_author_meta( 'wcb2b_group', $current_user->ID );
    
    		$restricted_file  = get_sub_field('restricted_file');
    		$non_restricted_file  = get_sub_field('non_restricted_file');
    
    		if( array_intersect($author_roles, $user->roles ) ) : ?>
    		<li><a href="<?php echo $restricted_file['url']; ?>"><?php echo $restricted_file['filename']; ?></a></li>
    		<?php endif; ?>
    		
    		<li><a href="<?php echo $non_restricted_file['url']; ?>"><?php echo $non_restricted_file['filename']; ?></a></li>
    
    	<?php endwhile;
    	echo '</ul>';
    endif;

    You’ve not included all of your code! You haven’t shown the file fields being declared so made up some for the example.

    Does that help?

  • Sorry, but no. In what way is

    if( array_intersect($author_roles, $user->roles ) ) :

    looking for the restricted role on the files?

  • By editing this line:
    $author_roles = array('administrator', 'editor', 'contributor');

    So if the logged in user role is not specified here, they don’t see the heading or restricted files.

  • Yes but that’s not what I’m trying to do. I’m trying to see if there are any files restricted to my user role in the loop. nevermind if they are logged in or not. (of course, they are, but that is not the point.)

  • Have you even tried the code?

    I’m sorry, I’m doing my best to help you but you’re not really helping yourself here.

    Your topic title doesn’t match your question
    Your initial question is a bit vague and mentioned hiding the heading – which I did
    Your initial code is not complete

    The code I’ve provided hides the title based on a user role – you restrict it by amending which roles are in the array
    It also hides specific files based on the user role

    You’ve not really provided any other details, so trying to provide a solution without all the information.

    Unlike a WP Query, you can’t query a repeater as such. Therefore, you need to loop through the repeater, if it meets your requirements i.e. visible to specific roles, then show the files.

    For people to assist you, you need to assist them! Provide a clearer question, provide your code and outline exactly what you wish to achieve.

    Based on the information provided, the supplied code does what you’ve asked – hides the heading and files based on a user role.

  • I think I understand the question.

    You want to show the entire thing, including the header, if any of the lines in the repeater are for the user.

    I would use a PHP output buffer like this

    
    ob_start();
    $has_output = false;
      
    echo '<h3>' . $download_headline . '</h3>';
    
    while ( have_rows('section_download', 'options_customer') ) : the_row();
    
        //File restricted to
        $file_restriction  = get_sub_field('role_restriction');
        //Current user group
        $wcb2b_current_user_group = get_the_author_meta( 'wcb2b_group', $current_user->ID );
        
        if( in_array($wcb2b_current_user_group, $file_restriction ) ) { 
           $has_output = true;
           // generate content
        }
    
    endwhile;
    
    $output = ob_get_clean();
    if ($has_output) {
      echo $output;
    }
    
  • @hube2 glad you understood it!

    Sorry I was unable to assist…

  • Yes, That is exactly how I meant! Thank you. The only thing now is that I realized I will have to make a nested repeater to get several download sections with different headlines. I’m not 100% sure I know how to solve that. Can you help me? This is my code as it looks right now. The headline field is in the first repeater “file_sections”.

    $current_user = wp_get_current_user();
    
        echo '<div class="download_wrapper">';
    
            if( have_rows('file_sections', 'options_customer') ):
    
                while ( have_rows('file_sections', 'options_customer') ) : the_row();
    
                    echo '<div class="file-section">';
    
                        $download_headline = get_sub_field('download_headline', 'options_customer');
                        $download_preamble = get_sub_field('download_preamble', 'options_customer');
    
                        if ( $download_headline ) {
                            echo '<div class="file-title-section">';
                                echo '<h3>' . $download_headline . '</h3>';
                                echo '<p>' . $download_preamble . '</p>';
                            echo '</div>';
                        }
    
                        if( have_rows('section_download', 'options_customer') ):
    
                            ob_start();
                            $has_output = false;
    
                            echo '<ul class="download-list">';
    
                                while ( have_rows('section_download', 'options_customer') ) : the_row();
    
                                    //The file
                                    $file              = get_sub_field('client_file');
                                    //File restricted to
                                    $file_restriction  = get_sub_field('role_restriction');
                                    //Current user group
                                    $wcb2b_current_user_group = get_the_author_meta( 'wcb2b_group', $current_user->ID );
                                    
                                    if( in_array($wcb2b_current_user_group, $file_restriction ) || empty($file_restriction) ) { 
                                       $has_output = true;
                                       
                                       echo '<li class="download-item animateUp">';
    
                                            echo '<a target="_blank" title="Ladda ner fil: ' . $file['title'] . '" alt="Ladda ner fil: ' . $file['title'] . '" href="' . $file['url'] . '"></a>';
    
                                            echo '<div class="item-wrapper">';
                                                if( $file['type'] == 'image' ):
                                                    echo '<div class="item-image">';
                                                        echo '<img src="' . $file['sizes']['thumbnail'] .'" alt="'. $file['alt'] . '" width="50" height="auto">';
                                                    echo '</div>';
                                                endif;
                                                
                                                echo '<div class="item-content">';
                                                    echo '<h3>' . $file['title'] . '</h3>';
                                                    echo '<span>' . $file['modified'] . '</span>';
                                                echo '</div>';
                                            echo '</div>';
    
                                            $hr_size = size_format( $file['filesize'] );
                                            echo '<div class="item-size">' . $hr_size .'</div>';
    
                                        echo '</li>';
                                    }
    
                                endwhile;
    
                            echo '</ul>';
    
                            $output = ob_get_clean();
                            if ($has_output) {
                                echo $output;
                            }
    
                        endif;
    
                    echo '</div>';
    
                endwhile;
    
            endif;
    
        echo '</div>';
  • oh, I just realized I could actually fetch that headline from the nested repeater. Works exactly as I wanted it to now! Thank you so much.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.