简体   繁体   中英

Impossible to filter custom post in query using tax_query

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

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),
    )
  )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM