How to create filterable portfolio in WordPress?

You’re setting $terms_portfolio too early. You’re using get_the_terms( get_the_ID(), 'portfolio_categories'), but get_the_ID() won’t be the current portfolio in the loop because $portfolio_query->the_post() hasn’t run yet.

Change this bit:

<?php $portfolio_query = new WP_Query(array(
          'post_type' => 'portfolios',
          'order' => 'DESC',
          ));
        $terms_portfolio = get_the_terms( get_the_ID(), 'portfolio_categories');
        ?>     
        <?php if($portfolio_query->have_posts()) : while($portfolio_query->have_posts()) : $portfolio_query->the_post();  ?>
        <div class="col-md-4" data-tag="<?php echo $terms_portfolio[0]->slug; ?>">

To:

<?php 
$portfolio_query = new WP_Query(array(
    'post_type' => 'portfolios',
    'order' => 'DESC',
));

if($portfolio_query->have_posts()) : 
    while($portfolio_query->have_posts()) : $portfolio_query->the_post();  
        $terms_portfolio = get_the_terms( get_the_ID(), 'portfolio_categories'); 
        ?>

        <div class="col-md-4" data-tag="<?php echo $terms_portfolio[0]->slug; ?>">

Leave a Comment