Home › Forums › Front-end Issues › Relating posts with Woocommerce products › Reply To: Relating posts with Woocommerce products
Ah, that makes sense. So this is rather a reverse relationship query. In that case your friend was correct, but it can be done in a single query:
<?php
$related_posts = get_posts(array(
'post_type' => 'post',
'numposts' => -1,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'related_products',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
),
)
));
?>
<?php if ( $related_posts ): ?>
<ul class="medium-block-grid-5 blog-posts">
<?php foreach( $related_posts as $rp ): ?>
<li class="post">
<a href="<?php echo get_permalink( $rp->ID ); ?>" title="<?php the_title_attribute( array( 'post' => $rp->ID ) ); ?>" ><span class="featured-title"><h3><?php echo get_the_title( $rp->ID ); ?></h3></span></a>
<?php if ( has_post_thumbnail( $rp->ID ) ) : ?>
<?php echo get_the_post_thumbnail( $rp->ID, 'regular-posts' ); ?>
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/img/no-image.png" alt="" />
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.