Skip to content

Instantly share code, notes, and snippets.

@ashleycam3ron
Last active March 4, 2020 19:08
Show Gist options
  • Save ashleycam3ron/7db3b02b461be1614faedd019ed87fe3 to your computer and use it in GitHub Desktop.
Save ashleycam3ron/7db3b02b461be1614faedd019ed87fe3 to your computer and use it in GitHub Desktop.
Need to check if a user is logged in, then whether their products (in an ACF repeater) are in the loop of firmware posts (on this archive). The firmware CPT has an ACF relationship field to its products.
<?php if ( is_user_logged_in() ) { ?>
<h1>Firmware Updates</h1>
<?php $current_user = wp_get_current_user(); ?>
<h3 style="margin-bottom: 6px;">Welcome, <?php echo $current_user->user_firstname; ?>!</h3>
<p>Your registered product software and updates are listed below.<br/>
Need to register another product on your account? Please fill out a <a data-fancybox href="#updateaccount">request form</a>.</p>
<?php // Software notes
if( have_rows('products', $userID) ): ?>
<?php while( have_rows('products', $userID) ): the_row();
$product = get_sub_field('product'); //get repeater post object
if ($product):
$productpost = $product;
setup_postdata( $productpost ); ?>
<?php //while there are software update posts
while ( have_posts() ) : the_post(); ?>
<?php if (!in_category( 'archive' )){ ?>
<?php // Get product update product_relationship
$relationship = get_field('product_relationship');
if( $relationship ):
$i = 0;
foreach( $relationship as $product) : ?>
<?php //if user product ID equals firmware relationship ID
if ( $productpost->ID === $product->ID){
$productID = $product->ID;
$note = get_field('notes');
if ($note){ ?>
<?php if ($i == 1){?>
<div class="notes clear">
<h3>Software Notes</h3>
<?php } ?>
<?php echo $note;?>
<?php if( $i == ( count( $relationship ) + 1 ) ){ ?>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
<?php endforeach; ?>
<?php endif; //end product_relationship ?>
<?php } //end check for archive ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); //reset user productpost setup ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<h1 style="margin-bottom: -20px;">Software Downloads</h1>
<?php // Get products user is registered for
if( have_rows('products', $userID) ): ?>
<?php while( have_rows('products', $userID) ): the_row();
$product = get_sub_field('product'); //get repeater post object
if ($product):
$productpost = $product;
setup_postdata( $productpost );
$i = 0; ?>
<?php //while there are software update posts
while ( have_posts() ) : the_post(); ?>
<?php // Get product update product_relationship
$relationship = get_field('product_relationship');
if( $relationship ): ?>
<?php foreach( $relationship as $product): ?>
<?php //if user product ID equals software relationship ID
if ( $productpost->ID === $product->ID){
$productID = $product->ID; ?>
<?php if (!in_category( 'archive' )){ ?>
<article class="updates">
<?php get_template_part( 'template-parts/product-updates' ); ?>
<?php get_template_part( 'template-parts/firmware-sdp' ); ?>
</div>
</article>
<?php } ?>
<?php } ?>
<?php endforeach; ?>
<?php endif; //end product_relationship ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); //reset user productpost setup ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php //rewind_posts(); ?>
<?php //reset_rows(); ?>
<h1>Archive</h1>
<?php if( have_rows('products', $userID) ):
while( have_rows('products', $userID) ): the_row();
$product = get_sub_field('product'); //get repeater post object
if ($product):
$productpost = $product;
setup_postdata( $productpost ); ?>
<?php //while there are software update posts
while ( have_posts() ) : the_post();
if (in_category( 'archive' )){ ?>
<?php // Get product update product_relationship
$relationship = get_field('product_relationship');
if( $relationship ): ?>
<?php foreach( $relationship as $product): ?>
<?php //if user product ID equals software relationship ID
if ( $productpost->ID === $product->ID){
$productID = $product->ID;
echo $post->ID;?>
<div class="accordion updates">
<header>
<h2><?php the_title(); ?></h2>
<?php if ( get_field('release') ){ ?>
<span>Released <?php the_field('release'); ?></span>
<?php } ?>
</header>
<div class="body">
<?php if ( get_field('notes') ){ ?>
<h3>Software Notes</h3>
<?php the_field('notes'); ?>
<?php } ?>
<?php get_template_part( 'template-parts/product-updates-archive' ); ?>
<?php get_template_part( 'template-parts/firmware-sdp' ); ?>
<h4>User Documentation</h4>
<ul>
<!-- !User Guides Archive[edit: Product]-->
<?php get_template_part( 'template-parts/firmware-user-guides-archive' ); ?>
</ul>
</div>
</div>
<?php } ?>
<?php endforeach; ?>
<?php endif; //end product_relationship ?>
<?php } //end check for archive ?>
<?php endwhile; ?>
<?php //rewind_posts(); ?>
<?php wp_reset_postdata(); //reset user productpost setup ?>
<?php endif; ?>
<?php endwhile; endif;?>
<?php } //end if user logged in?>
@realj42
Copy link

realj42 commented Mar 3, 2020

Hi Ashley
For the Heading 'Software Notes' appearing twice you have made a classic error on line - <?php if ($i = 1){?> should be <?php if ($i == 1){?> - == not = ! . Also you never manipulate this variable so just that change will not work. Line 27 shoud be $i = 1; then line 42 should be <?php $i++; } ?> - increment $i so you dont put out the heading on the next loop.

Ok let me look at your other issue ...

@realj42
Copy link

realj42 commented Mar 3, 2020

Ok that second major loop is making my head spin! Been a long day and the football is on now so maybe I can look tommorrow!

@ashleycam3ron
Copy link
Author

ashleycam3ron commented Mar 4, 2020

@realj42 Thank you so much!! I've been smashing my brain on this thing for hours on different days. So much appreciate your time to look and comment. Do you know how I might end that div on Line 46 properly to wrap the heading and note items in just one div?

@realj42
Copy link

realj42 commented Mar 4, 2020

If you mean you want one div for all notes and the heading, and you can't just declare the <div> & </div> at lines 28 & 49 resp. (I assume that would create an empty box if no, uh, relationships) then you cannot do it with your current approach, using inline html. Really what you need to do is build each section as seperate variables then put them together once they are all built, deciding then if you need the section divs if the corresponding variable has some content.

Sorry bit too busy to fully explain right now but catch me next week if you still need some help!

@ashleycam3ron
Copy link
Author

@realj42 - you were much quicker on responding than I anticipated. I just updated - think I found a solution. I'm still having the issue with the archives repeating. Understand and appreciate your response! Take care. I'll comment back if this isn't resolved.

@ashleycam3ron
Copy link
Author

@realj42 It's a miracle! Got the archive duplicate solved. I moved the while and if on Lines 115 & 116 inside the relationship after Line 126. Thank goodness I can move on to something else now!! I appreciate your help again. Thank you. I'm still not sure I really solved the end div inside the software notes but I'll try that another day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment