I have various products (custom post type) which already have categories but I have now I have added this line to be able to add tags to the products.
'taxonomies' => array('post_tag')
So, I have an industries section with various pages within that and for example one of them is called 'Woodworking'. I have added the tag 'Woodworking' to various products and when someone visits /industry/woodworking I want those products to show up.
Is there an easy way to achieve this? I thought of perhaps using WP_Query
but not sure how to create that relationship between the page name and the product tags.
EDIT:
I have done this which seems to work but not sure if the right way to go about it?
<?php $page_title = get_the_title(); ?>
<?php
$tag_prods = new WP_Query(array(
'tag' => 'Woodworking',
'posts_per_page' => -1,
'post_type' => 'Products',
'order' => 'ASC'
));
?>
<?php if ($tag_prods->have_posts() ): while ($tag_prods->have_posts() ): $tag_prods->the_post(); ?>
<?php foreach(get_the_tags() as $tags): ?>
<?php if ($tags->name === $page_title): ?>
<p><?php the_title(); ?></p>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
<?php endif; ?>
Just noticed that this only works with single words. As soon as a tag word has a space in it then it doesn't pick it up.
tag
argument expects one or more tag slugs (e.g.tag-name
and notTag Name
). See here for the full arguments list.