Home > Blockchain >  Impossible to filter custom post in query using tax_query
Impossible to filter custom post in query using tax_query

Time:02-02

First time than I use Timber and I'm looking for help to find a way to filter some custom_post with a taxonomy.

In this file single-news.php, I try to display in my custom posts (news) controller a link with a an acf taxonomy field named 'categorie_dexpertise' When I get it I use it in tax_query ... I try to filer 'collaborateur' post type with term_id 'categorie_dexpertise' in 'expertise-collaborateur' taxonomy But it doesn't work!

Single-news.php

    <?php 

$context = Timber::get_context();
$post = Timber::query_post();


$context['post'] = $post;
$context['term'] = new Timber\Term('category-news', 'news');

$expertise_team = get_field('categorie_dexpertise');
$context['expertise_team'] = $expertise_team;

$args = array(
    'post_type'        =>  'collaborateur',
    'post_status'       => 'publish',
    'order'             => 'DESC',
    'tax_query' => array(
      'taxonomy' => 'expertise-collaborateur',
      'field'    =>  'term_id',
      'terms'   =>  array(11),
    ),
  );
$context['teamfilter'] =  Timber::get_posts($args);

Timber::render('single-news.twig', $context);

Is there a better way or an alternative way to filter my custom-posts? Thank for your help :) :)

CodePudding user response:

The tax_query parameter should always be an array of arrays. Try to use this code instead:

<?php
$args = array(
  'post_type'        =>  'collaborateur',
  'post_status'       => 'publish',
  'order'             => 'DESC',
  'tax_query' => array(
    array(
      'taxonomy' => 'expertise-collaborateur',
      'field'    =>  'term_id',
      'terms'   =>  array(11),
    )
  )
);

CodePudding user response:

Thanks for your feedback. But it's still not filter... In code I put the id 11 return by $expertise_team

<?php
$args = array(
  'post_type'        =>  'collaborateur',
  'post_status'       => 'publish',
  'order'             => 'DESC',
  'tax_query' => array(
    array(
      'taxonomy' => 'expertise-collaborateur',
      'field'    =>  'term_id',
      'terms'   =>  array($expertise_team),
    )
  )
);
  •  Tags:  
  • Related