We are using relationship tables and are populating the selecting list by drop. While we are able to display the list properly sorted in the front-end based on the selected list in the admin page, we have a requirement to display all those products which are marked as discontinued at the bottom. Can you please help me with this.
Hi @arakesh
For this kind of case, you need to check if the posts in the loop are discontinued or not. You can do it by using the true/false field in the posts you added to the relationship field. After that, you can put the discontinued posts in a variable and then print it later. It should be something like this:
$the_posts = get_field('relationship_field_name');
// Create a variable to contain the discontinued posts
$discontinued_posts = array();
if( $posts ): ?>
<ul>
<?php foreach( $the_posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
// Check if the posts is discontinued or not
if( get_field('discontinued_field_name') ){
// Put it in the array for later use.
$discontinued_posts[] = $post;
continue;
}
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span>Custom field from $post: <?php the_field('author'); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php
// Now we print the discontinued posts
if( $discontinued_posts ): ?>
<ul>
<?php foreach( $discontinued_posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span>Custom field from $post: <?php the_field('author'); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
I hope this helps 🙂