Join WhatsApp ChannelJoin Now

How To get Related Products In WordPress

Hello Dev,

Today, i we will show you how to get related products in wordpress. This article will give you simple example of how to get related products in wordpress. you will learn how to get related products in wordpress.

So let’s follow few step to create example of how to get related products in wordpress.

Example

global $post;
$related = get_posts( 
  array( 
  'category__in' => wp_get_post_categories( $post->ID ), 
  'numberposts'  => 4, 
  'post__not_in' => array( $post->ID ),
  'post_type'    => 'product'
  ) 
);
  if( $related ) { 
    foreach( $related as $post ) {
        setup_postdata($post); 
        $thumbnail = wp_get_attachment_url( get_post_thumbnail_id(get_the_ID()), 'full' );
        $product = wc_get_product( get_the_ID() );
          $permalink = get_the_permalink(get_the_ID());
          $title = get_the_title();
          $price = $product->get_price();
     }
 wp_reset_postdata();
}

Recommended Posts