Luc_Gibson
Luc_Gibson

Reputation: 47

Wordpress while loop to only get certain term from a custom taxonomy

I am trying to create a custom page template that pulls through a list of posts under a single taxonomy term.

I have a custom post type (download) and a custom taxonomy (download_type)

I want the following code to only pull through posts within the term 'mockup' from the download_type taxonomy.

<?php $args = array (
'post_type' => 'download'
);
$the_query = new WP_Query($args);
?>

<?php $i = 1; ?>
<?php  if ($the_query->have_posts() ): while ($the_query->have_posts() && $i < 5) :$the_query->the_post(); ?>

<a href="<?php the_permalink(); ?>">
<div class="fullwidth_download" style="background-image: url('<?php the_post_thumbnail_url(); ?>')">
<div class="item_meta">
<h2><?php the_title(); ?></h2>
<h3 class="item_price">£<?php the_field('item_price'); ?></h3>
<?php
// vars 
$file_type = get_field('file_type');
// check
if( $file_type ): ?>
<ul class="file_types">
<?php foreach( $file_type as $file_type ): ?>
<li><?php echo $file_type; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>

Upvotes: 0

Views: 710

Answers (1)

cabrerahector
cabrerahector

Reputation: 3948

You can use the tax_query parameter for this:

<?php
$args = array(
    'post_type' => 'download',
    'tax_query' => array(
        array(
            'taxonomy' => 'download_type',
            'field'    => 'slug',
            'terms'    => 'mockup',
        ),
    ),
);
$the_query = new WP_Query( $args );

// Rest of the code here...

Upvotes: 1

Related Questions