I have used this technique before and cannot work out why this time it is not working.
'news_category' is a custom taxonomy of my custom post type 'News'.
I am getting the taxonomy of the current post (which is custom post type 'news') using get_the_term_list, replacing the spaces with '-' and using the variable in my query.
If I echo the query it gives me the exact string I am expecting - and if I manually enter this string into the query, it works, but the variable doesnt.
$cats = get_the_term_list( $post->ID, 'news_category' );
$cat = str_replace( ' ', '-', $cats );
$category = strtolower( $cat );
echo $category;
$args = array(
'post_type' => 'news',
'news_categories' => $category,
'post__not_in' => array( $post->ID )
);
$sidebar_category = new WP_Query( $args );
I have used this technique before and cannot work out why this time it is not working.
'news_category' is a custom taxonomy of my custom post type 'News'.
I am getting the taxonomy of the current post (which is custom post type 'news') using get_the_term_list, replacing the spaces with '-' and using the variable in my query.
If I echo the query it gives me the exact string I am expecting - and if I manually enter this string into the query, it works, but the variable doesnt.
$cats = get_the_term_list( $post->ID, 'news_category' );
$cat = str_replace( ' ', '-', $cats );
$category = strtolower( $cat );
echo $category;
$args = array(
'post_type' => 'news',
'news_categories' => $category,
'post__not_in' => array( $post->ID )
);
$sidebar_category = new WP_Query( $args );
Share
Improve this question
edited Oct 25, 2020 at 2:44
admcfajn
1,3262 gold badges13 silver badges30 bronze badges
asked Jun 26, 2014 at 9:17
DJCDJC
931 silver badge8 bronze badges
1
- You want get all category term in current post – HK89 Commented Oct 25, 2020 at 3:15
2 Answers
Reset to default 1You have to use array like this:
array(
'post_type' =>'news',
'category_name' => $category, //use category slug (NOT name).
'post__not_in'=> array($post->ID)
);
For more reference please visit this link
The variable is not working because get_the_term_list
returns a string of HTML links, whereas your $args
array requires slugs. I'm not sure if you can pass an array of slugs using your current method or not. You can certainly pass an array of slugs using a tax_query array.
Incidentally, using your current method, you should be setting the tax name to 'news_category', not 'news_categories', but this is moot.