0

I have a custom post tipe ("artigo"), and this CPT has a registered taxonomy ("artigo_eixo"), with 3 registered terms in it.

I want to show, in that page, a sidebar listing all terms, except the current term, in the page that lists all posts with that term (http://localhost/mytaxonomy/current_term/)

However, I can't exclude the current term from the query.

I'm trying to get a list of taxonomy terms, but exclude a certain term from this list. I'm trying to follow the codex examples, but I'm having no success, using either get_terms() or WP_Term_query().

There are 3 terms and the IDs are 13,14,15. There are no posts or CPT items with those IDs.

I just tested get_queried_object_id() and it seems to be returning the proper term IDs of the term being shown -- that is, if I am viewing the URL of the term with ID 13, the function returns 13, and so on.

It also doesn't work using hardcoded values, be it a string, an integer, or an array of any of those types. Neither won't work:

'exclude' => 14
'exclude' => '14'
'exclude' => array(14)
'exclude' => array('14')

There are no errors displayed by PHP or the WP debug log.

get_terms

if (is_tax( 'artigo_eixo' )) {

    $current_eixo = get_queried_object_id();

    $args = array(
        'taxonomy' => 'artigo_eixo',
        'exclude' => $current_eixo
    );

    $eixos = get_terms( $args );

WP_Term_query

if (is_tax( 'artigo_eixo' )) {

    $current_eixo = get_queried_object_id();

    $args = array(
        'taxonomy' => array('artigo_eixo'),
        'exclude' => $current_eixo
    );

    $eixos = new WP_Term_Query( $args );
    $eixos = $eixos->terms;
0

Turns out there was a function hooking the filter list_terms_exclusions, and the return was inside a logically faulty conditional. I fixed it and now the term query exclusion works as intended.

  • I'm having this exact same issue, it would be nice if your answer had more details on how you fixed it. – AyexeM Nov 30 '17 at 5:04
  • @AyexeM You're totally right. Sorry for that. I have to check the logs of the git repository I was using back then. I might have something tomorrow. – That Brazilian Guy Nov 30 '17 at 5:43
-1

Possible reason: get_queried_object_id() returns int, whereas exclude parameter uses array or string of comma separated ids.

First make sure you are getting right value in $current_eixo, and then try putting it in array and using that array as parameter.

'exclude' => array($current_eixo)

Although not sure if this is the problem in your case, give it a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.