How to get term slug with dynamic data?

Is there a DD that will return a custom taxonomy term’s slug assigned to a post? For context, I want to use it in a query loop like so:

Sidenote: I’m using Bricks with Metabox Relationships.

return [
	'relationship' => [
		'id' => 'testimonials-services',
		'to' => {post_id},
		], 
	'post_type' => 'testimonial',
	'tax_query' => [
		'relation' => 'AND',
		[
			'taxonomy' => 'topic',
			'field' => 'slug',
			'terms' => '**{POST TERM SLUG FOR TAXONOMY HERE}**',
			'include_children' => false, 
			'operator' => 'IN'
		],
	],
];

What I’ve tried but it didn’t work

{post_terms_topic} was a DD that was created in Bricks when I created the 'topic' taxonomy with Metabox. I’ve tried testing the output of this DD in a Basic Text element like so:

  1. {post_terms_topic} = returns Topic Name wrapped in a link (but not the slug)
  2. {post_terms_topic:value} = returns Topic Name wrapped in a link (but not the slug)
  3. {post_terms_topic:raw} = returns {post_terms_topic}
  4. {post_terms_topic:plain} = returns Topic Name in plain text, but not the slug

I’ve even tried experimenting like so:
5. {post_terms_topic:slug} = returns nothing at all

@Sridhar might you have any experience with this or something on BricksLabs talking about this?

Tried this?

Yes I tried that. That’s the {post_terms_topic} DD tag in this case. But it only returns the term name, not the term slug. I’m trying to get it to return the term’s slug.

Hey @michaelkern,

if it does not have to be a dynamic data tag you can use your own custom function (including the echo: tag if needed):

function get_the_term_slugs( $taxonomy ) {
    return implode( ', ', wp_list_pluck( get_the_terms( get_the_ID(), $taxonomy ), 'slug' ) );
}

/* Allow function to be used with the echo tag if needed */
add_filter( 'bricks/code/echo_function_names', function() {
    return [
        'get_the_term_slugs',
    ];
} );

Best,

André

1 Like

Fantastic! Thank you! This output exactly what I want.

I’ve still got an issue though, which is trying to get the output to “play nice” with the PHP Query Loop Builder which is returning nothing when I try to use the function in my query as follows:


$post_term = get_the_term_slugs( 'topic' );
return [
	'post_type' => 'testimonial',
	'tax_query' => [
		'relation' => 'AND',
		[
			'taxonomy' => 'topic',
			'field' => 'slug',
			'terms' => $post_term,
			'include_children' => true, 
			'operator' => 'IN'
		],
	],
];

I’m not entirely sure what I’m doing wrong. I also tried wrapping it in single quotes (escaped) as follows but then I get a parserError:


$post_term = get_the_term_slugs( 'topic' );
return [
	'post_type' => 'testimonial',
	'tax_query' => [
		'relation' => 'AND',
		[
			'taxonomy' => 'topic',
			'field' => 'slug',
			'terms' => \'$post_term\',
			'include_children' => true, 
			'operator' => 'IN'
		],
	],
];

Not sure what I’m missing here. I’m so close to getting it to do what I want, but just an inch short.

First make sure get_the_terms_slugs() is actually giving you a value!

echo '<pre>';
  var_dump($post_term) # shows true / false if nothing
// print_r($post_term) # if returns, easier to read
echo '</pre>';

Do you just need to pull in one single term (the first one), or an array?

If you just need the first term, you can just use:

$post_term = get_the_terms(get_the_ID(), 'your_tax')[0]->slug

You should then be able to use $post_term as you’ve shown…

I also noticed you did not use ‘posts_per_page’ => … not sure if it matters.

Here’s a similar example I’m using, and works good!:

I added your post type for example here instead of what I use

return [
  'post_type' => 'testimonial',
  'posts_per_page' => -1, // -1 for all, or set number
  'tax_query' => [
    [
      'taxonomy' => 'topic',
      'field' => 'slug',
      'terms' => 'ebook',
    ]
  ]
];

Double triple check your actual taxonomy is correct!

1 Like

Yup the var_dump and print_r return the intended string. I’m getting the string I need, it just doesn’t work in the PHP Query Loop Builder. Weird, right?

Everything works correctly up until the point that I try to use the variable that calls the function in the 'terms' => $post_term part of the query.

My hypothesis is that it’s probably not working in the QL Builder because it’s expecting a term to be wrapped in single quotes.

Thing is, I wrote a function that forced the output to be returned wrapped in quotes, and even that didn’t work in the QL Builder.

I then tried another way—using backslashes \ to escape the single quotes in the QL Builder ('terms' => \'$post_term\', and all I got is a parserError.

Yup my taxonomy is topic

Understood, I would then try to create a function like @aslotta mentioned, and get your code working first that way.

Then you can 100% verify it’s Bricks !

I found an easy way is to use the add_shortcode() to avoid the new Bricks echo filter stuff.

add_shortcode('bricksTest', function() {

$query = your query...

return '<pre>'.$query.'</pre>';

});

[bricksTest]

or… do_shortcode(‘[bricksTest]’);

or… in bricks {echo:bricksTest()}

Keep sharing what returns for you!

Whoa whoa whoa! Hold up. Hold up. I’ve overlooked something.

It’s not working in the Builder. But it’s working in the front-end. I’ve confirmed it’s querying correctly on the front-end by verifying with Query Monitor.


For context, the term ID is 9, so it’s targeting what it is supposed to!

Sorry for the flag on the play! But yeah, while it works on the frontend, the builder doesn’t show an output of any query results.

1 Like

It’s all about testing what you know works and finding what ISN’T.

Good stuff!

My only idea here is in Bricks settings → Builder, then look for “Dynamic data” section (maybe “Render dynamic data text on canvas”)… other than that, hopefully someone else can help you along:

Hey @michaelkern,

instead of calling the function try to insert the function’s code in the query editor itself. For me that way it works in the builder as well:

$term_slugs = wp_list_pluck( get_the_terms( {post_id}, 'topic' ), 'slug' );

return [
  'post_type' => 'testimonial',
  'tax_query' => [
    [
      'taxonomy' => 'topic',
      'field' => 'slug',
      'terms' => $term_slugs,
    ]
  ]
];

Best,

André

I’m a PHP amateur, and trying to educate myself. One of the things I would like to master is writing my own queries with the query editor. I’ve instructed chatGPT to teach me.

In pages I have created for the second lesson, this code works just fine:

$args = [
  'post_type' => ['trainings', 'conferences'],
  'posts_per_page' => 10,
  'meta_query' => [
    [
      'key' => 'training_start_date',
      'value' => date('Ymd'),
      'compare' => '>=',
      'type' => 'NUMERIC'
    ],
    'relation' => 'OR',
    [
      'key' => 'conference_start_date',
      'value' => date('Ymd'),
      'compare' => '>=',
      'type' => 'NUMERIC'
    ],
  ],
  'orderby' => 'meta_value',
  'meta_key' => ['training_start_date', 'conference_start_date'],
  'order' => 'ASC',
];

return $args;

So, there’s nothing wrong with the post type, or with code execution or anything like that.
But now comes the problem, and the reason why I’m replying here: I’m in the third lesson now, which is basically only adding a tax_query:

$args = [
  'post_type' => ['trainings', 'conferences'], // Haal beide CPTs op
  'posts_per_page' => 10, // Beperk het aantal resultaten
  'tax_query' => [
    [
      'taxonomy' => 'technology', // De naam van de custom taxonomie
      'field'    => 'slug', // We gebruiken de slug van de term
      'terms'    => 'ai', // Filter op de term 'AI'
    ],
  ],
  'meta_query' => [
    'relation' => 'OR',
    [
      'key' => 'training_start_date',
      'value' => date('Ymd'),
      'compare' => '>=',
      'type' => 'NUMERIC',
    ],
    [
      'key' => 'conference_start_date',
      'value' => date('Ymd'),
      'compare' => '>=',
      'type' => 'NUMERIC',
    ],
  ],
  'orderby' => [
    'conference_start_date' => 'ASC',
    'training_start_date' => 'ASC',
  ],
];

return $args;

This returns absolutely nothing, even if there are at least 4 posts that have the tax term ‘ai’ associated with them.

I then went back to basics and copied what you guys were suggesting here, but even a query like this returns nothing:

return [
  'post_type' => 'trainings',
  'posts_per_page' => -1, // -1 for all, or set number
  'tax_query' => [
    [
      'taxonomy' => 'technology',
      'field' => 'slug',
      'terms' => 'ai',
    ]
  ]
];

I’m a bit at my wits end, and deeply disappointed in my chatGPT’s mentoring skills :slight_smile:

Can a human please explain why the code above returns nothing? I would really like to know, and learn, so I really would appreciate anything you can throw at me.

Figured it out -the simplest of things. In ACF my tax terms were not set to save to the post. Embarrasing. :man_facepalming: