I have a custom-post-type named my_projects
and a custom-taxonomy called project_tags
.
I want to write a function that is returning 5 posts of this post-type based on a given taxonomy term.
So I want to call <?php get_related_projects("Car"); ?>
and get 5 posts that also share the tag
car returned.
This is my current attempt (that doesn't work)
function get_related_projects( $term ) {
echo '<ul class="featured-items">';
$args = array(
'post_type' => array('my_project'),
'order' => 'DSC',
'post_count' => 5,
'meta_key' => 'project_tags',
'meta_compare' => $term,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $post;
?>
<li>
<figure class="project-image">
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
</figure>
<h1 class="title"><a href="<?php the_permalink(); ?>"><span class="goto">a</span> <?php the_title(); ?></a></h1>
</li>
<?php
endwhile;
wp_reset_postdata();
echo '</ul>';
}
Since I'm not an expert when it comes to custom loops I wonder what I'm doing wrong here? Thank you for your help.
UPDATE:
<?php if ( get_related_projects( "Car" ) ): ?>
<em>Related Projects</em>
<?php get_related_projects("Car"); ?>
<?php endif; ?>