I’m building a woocommerce theme from scratch and I’m trying to figure out how I can display a custom post type using product tags. For example I have apple as a product and I use the word “apple” as a product tag.
I have then a custom post type called recipes. Let just say I publish a post called “apple salad” then I tag it with the word “apple” also.
I want to display the custom post type**(recipes)** based on the product tag. If there are any product tags with the word apple under the recipes custom post type it will then be display. I have this code that is under work in progress and still trying to figure out which parts to change. I’m not very fluent with PHP and I have been researching for any post that is similar to my problem
<?php //Related Post by Tag
$tags = wp_get_post_tags($post->ID);
if ( $tags ) {
$first_tag = $tags[0]->term_id;
$args = array(
'tag__in' => array( $first_tag ),
'post_type' => 'recipes',
);
}
$related_post = new WP_Query( $args );
if( $related_post->have_posts() ) : while ( $related_post->have_posts() ) : $related_post->the_post();
?>
<div class="full-width related-post">
<span>Recommended</span>
<h2><?php the_title(); ?></h2>
</div>
<?php wp_reset_postdata(); ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>