Support

Account

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; ?>