AnswerBun.com

get_terms (or tax_query) for term of current post?

WordPress Development Asked by FNTC on October 18, 2020

I’ve seen many solutions but none seem to work for me… I’ve got a grid off the loop/content on single.php that renders the whole grid as is, so I use the function as below which works fine, but only if I specify the exact taxonomy term (client-1).

function my_query_args($query_args, $grid_name) {
if ($grid_name == 'client-grid') {
    $query_args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => 'client-1',
        ),
    );
}
return $query_args;
}
add_filter('tg_wp_query_args', 'my_query_args');

But I want the term of the current post instead so I thought that below would work, but it renders all posts in the grid no matter what I do.

function my_query_args($terms_list, $grid_name) {
if ($grid_name == 'client-grid') {

    $terms_list = get_terms
        (array(
            'taxonomy' => 'category',
            'parent' => 0,
        )
    );  
}
return $terms_list;

}
add_filter('tg_wp_query_args', 'my_query_args'); 

Thankful for any input.

EDIT

Thinking of something like this (which obvs doesn’t works), but anyone that can point in the right direction?

$term_id = get_queried_object_id();

function my_query_args($query_args, $grid_name) {
if ($grid_name == 'JTS-SINGLE') {

    $query_args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => $term_id
        ),
    );
}
return $query_args;

}
add_filter('tg_wp_query_args', 'my_query_args', 10, 2);

2 Answers

SOLVED

This was what I was looking for and it works like a charm.

function my_query_args($query_args, $grid_name) {

$term_list = get_the_terms( get_the_ID(), 'category' )[0]->slug;

if ($grid_name == 'JTS-SINGLE') {
    $query_args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( $term_list ),
        ),
    );
}
return $query_args;

}
add_filter('tg_wp_query_args', 'my_query_args', 10, 2);

Answered by FNTC on October 18, 2020

Have a look at wp_get_post_terms which allows you to specify a posiID, which is what I think you are getting at

Answered by essexboyracer on October 18, 2020

Add your own answers!

Related Questions

List posts based on first letter of posts

2  Asked on September 13, 2020 by shahinul-islam

   

How to redirect a page to another?

1  Asked on September 13, 2020 by heera

     

How to get the meta title of a page configured as blog (loop)

1  Asked on September 8, 2020 by gerard

 

I am trying to match the wpallimport using the Xpath Filter

1  Asked on September 4, 2020 by ankit-prajapati

     

how to list all post that are in the custom taxonomy using $wpdb

0  Asked on September 3, 2020 by kenneth-gervacio

   

How to pass multiple values in a form for tax_query?

0  Asked on August 21, 2020 by irishrunner16

   

How to add post_distinct filter to WP_Comment_Query?

2  Asked on August 17, 2020 by jonathan-gruber

   

How can I combine one field using wpdb and group by?

1  Asked on August 10, 2020 by user81828

   

Custom posts password protect

1  Asked on August 6, 2020 by jason

   

REST API: Display Category names in JSON?

1  Asked on August 3, 2020 by steve

       

Gutenberg withInstanceId. When to use it?

2  Asked on July 25, 2020 by at-least-three-characters

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP