-1

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 :) :)

DarkBee
  • 16,592
  • 6
  • 46
  • 58
dAvydAv
  • 9
  • 2

1 Answers1

0

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),
    )
  )
);
Artemy Kaydash
  • 459
  • 5
  • 4
  • Thanks for your feedback. But it's still not filter... In code I put the id 11 return by $expertise_team ``` 'collaborateur', 'post_status' => 'publish', 'order' => 'DESC', 'tax_query' => array( array( 'taxonomy' => 'expertise-collaborateur', 'field' => 'term_id', 'terms' => array($expertise_team), ) ) ); ``` – dAvydAv Feb 01 '22 at 23:33
  • @dAvydAv so, get_posts() returns nothing? – Artemy Kaydash Feb 01 '22 at 23:57
  • @Artermy it returns posts without filtering on taxonomy id term – dAvydAv Feb 02 '22 at 14:06
  • @dAvydAv are you sure that post_type, taxonomy, and term_id parameters provided in the code are correct? – Artemy Kaydash Feb 02 '22 at 15:13