You have a couple of issues here.
-
As your code stand,
$post
is undefined. It is actually better to make use ofget_the_ID()
to get the post ID instead of$post
-
wp_get_post_terms()
makes an extra db call, so if you are really set on performance, I would rather useget_the_terms()
-
You are returning term names, but then set the
field
parameter in yourtax_query
toslug
. The field value should match the value of the term passed. Just a note, never ever use thename
field in atax_query
, there is an issue with name sanitation in theWP_Tax_Query
class. If you are usingwp_get_post_terms()
, set thefields
parameter toids
to return an array of term ids
EXAMPLES
wp_get_post_terms()
$term_list = wp_get_post_terms(
get_the_ID(),
'topic',
array(
'fields' => 'ids'
)
);
if ( $term_list
&& !is_wp_error( $term_list )
) {
$args = array (
'post_type' => 'knowledge-base',
'orderby' => 'meta_value_num',
'meta_key' => 'top_four_num',
'tax_query' => array(
array(
'taxonomy' => 'topic',
'terms' => $term_list,
),
),
);
// Run your custom query here
}
get_the_terms()
$terms = get_the_terms(
get_the_ID(),
'topic'
);
if ( $terms
&& !is_wp_error( $terms )
) {
$term_list = wp_list_pluck( $terms, 'term_id' );
$args = array (
'post_type' => 'knowledge-base',
'orderby' => 'meta_value_num',
'meta_key' => 'top_four_num',
'tax_query' => array(
array(
'taxonomy' => 'topic',
'terms' => $term_list,
),
),
);
// Run your custom query here
}