0

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 2

1

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

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

5
  • Thanks, I've tried that as well (wp_get_post_terms i/o get_terms) but I get all posts as output there as well - for some reason it returns all categories i/o just the current posts term. I might miss something very obvs cause getting a bit of WP fatigue from this...
    – FNTC
    Apr 26, 2017 at 19:00
  • Can we have the template file code your loop is in? What is 'tg_wp_query_args' - is this related to theme.co/apex/forums/topic/… Apr 26, 2017 at 21:58
  • The grid is outside the loop and is called via <?php echo The_Grid('JTS-SINGLE'); ?> The query works fine with hardcoded terms, forexample 'terms' => 2, but I want the term to be dynamically return.
    – FNTC
    Apr 26, 2017 at 22:00
  • Thanks, this seems to be in the same ballpark https://theme.co/apex/forums/topic/offsetting-posts-with-the-grid-plugin/ but doesn't really help me. It's the same grid plugin.
    – FNTC
    Apr 26, 2017 at 22:06
  • Problem solved.
    – FNTC
    May 2, 2017 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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