Support

Account

Home Forums Front-end Issues Relating posts with Woocommerce products

Solved

Relating posts with Woocommerce products

  • Hi there – I was hoping someone might be able to help me with something I’ve been struggling with.

    I’m using WordPress + ACF + Woocommerce and I am trying to relate posts with certain products. I’ve successfully set up the relationship fields within the posts admin, so that for each post within the admin, I can select products which I want to associate to that post. This ACF is called “related_products” I’m having difficulty, however, displaying the results on the single product page.

    Alternatively, I’ve been able to display it in reverse, where I’ve set up an ACF called “related_posts” and I can select posts from the product admin page and then successfully show it on the single product page. I would rather set it up the other way – selecting products associated to the post on the post page.

    On my content-single-product.php page through woocommerce, here is the code I’m using:

    
    <?php $posts = get_field('related_products'); if( $posts ): ?>
        <ul class="medium-block-grid-5 blog-posts">
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <li class="post">
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><span class="featured-title"><h3><?php the_title(); ?></h3></span></a>
    			<?php if ( has_post_thumbnail() ) : ?>
    					<?php the_post_thumbnail('regular-posts'); ?>
    					<?php else : ?>
    					<img src="<?php echo get_template_directory_uri(); ?>/img/no-image.png" alt="" />
    					<?php endif; ?>
            </li>
        <?php endforeach; ?>
        </ul>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    

    In the other view, where I select post from each product within the admin, if I change get_field(‘related_products’) to get_field(‘related_posts’), all works well. I would like to set it up the other way, if possible.

    Can anyone advise me in what next steps I need to take? Thank you in advance for your help.

  • I’ve been working on this further, still not working, but wanted to put my updates here in case anyone sees anything obvious. What happens with this is that it displays the associated post, but each instance of it appears on the page. So if I select the same post for two products, on both of those product pages it appears twice. I feel like I’m close, but can’t figure it out.

    Thanks for your help.

    
    <ul class="medium-block-grid-5 blog-posts">
    <?php 
    $args = array( 'posts_per_page' => -1 );
    
    $fposts = get_posts( $args );
    foreach ( $fposts as $post ) :  ?>
    	<?php setup_postdata($post); ?>
    			
    		<?php $product = get_field('related_products'); if( $product ): ?>
    		<?php foreach( $product as $p ): ?>
            <li class="post">
    	        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><span class="featured-title"><h3><?php the_title(); ?></h3></span></a>
    			<?php if ( has_post_thumbnail() ) : ?>
    			<?php the_post_thumbnail('regular-posts'); ?>
    			<?php else : ?>
    			<img src="<?php echo get_template_directory_uri(); ?>/img/no-image.png" alt="" />
    			<?php endif; ?>
    		</li>
    		<?php endforeach; ?>
    		<?php endif; ?>
    								
    <?php endforeach; wp_reset_postdata();?>
    </ul>
    
  • Hi – Is there anyone who could help?

  • So this is assuming you’re using the Relationship field type with the return format set to Post Object:

    
    <ul class="medium-block-grid-5 blog-posts">	
    <?php if ( $products = get_field('related_products') ): ?>
      <?php foreach( $products as $p ): ?>
        <li class="post">
          <a href="<?php the_permalink( $p->ID ); ?>" title="<?php the_title_attribute( array( 'post' => $p->ID ) ); ?>" ><span class="featured-title"><h3><?php echo get_the_title( $p->ID ); ?></h3></span></a>
          
          <?php if ( has_post_thumbnail( $p->ID ) ) : ?>
            <?php echo get_post_thumbnail( $p->ID, 'regular-posts' ); ?>
          <?php else : ?>
            <img src="<?php echo get_template_directory_uri(); ?>/img/no-image.png" alt="" />
          <?php endif; ?>
        </li>
      <?php endforeach; ?>
    <?php endif; ?>
    </ul>
    

    Summary is that you don’t need to put this inside a WordPress loop, you can just iterate over the related posts and call the desired functions on that specific post (in this cast product) ID.

  • Hi ractoon!
    Thanks for responding to my issue. Thanks – I actually tried it that way as well, but it doesn’t yield any results, even though I have the product selected within the relationship block on the admin post page. And to confirm, yes, I’m using post object and the Relationship field type.

    I had a developer friend take a look and although he’s not able to help out with this at the moment, he thinks this is what needs to happen: It has to find the posts of the custom field, then find the id’s from those posts by querying the db. Then, it has to return an array of those fields to the single product template, and based on that array of Id’s, set up a WP Query for those posts.

    So this seems more complicated than I originally anticipated it being. If you have any further thoughts beyond this, I’m all ears. Thanks again for your willingness to respond to my issue! Much appreciated.

  • Certainly. That proposed solution does sound quite a bit more complex than necessary (essentially duplicating what ACF should be doing currently).

    If you do:

    
    $products = get_field('related_products');
    print_r($products);
    

    On a post you know to contain related products what do you get back?

  • Actually, I’m trying to display the related posts on the single product page, not the single post. So if I were to put the code you referenced above on the content-single-product.php page, nothing prints.

    Just in case my original post isn’t clear, the admin Relationship field is set up to appear on the admin post page. This is where I select all products associated to that particular post. Then I’m trying to display the results on the content-single-product.php page for each product that is associated to the post.

    So far what its resulting in (using the code I included in my second post) is the post appears on every product page, not only for each selected product. And it sometimes appears more than once.

    Again, thanks for your help!

  • 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; ?>
    
  • Oh my goodness – you are wonderful! This works! And now I understand what needed to happen. Thank you so very much for all your help.

    I hope this might help someone in the future!

  • Hello i know this is a old post. but i need help.

    The first code works for me. but i need to show the template of product. how do i do that? instead of showing the title one by one.

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

You must be logged in to reply to this topic.