I'm trying to get a list of categories if they are used in a Custom Post type. The post type uses the default categories taxonomy. There are also other custom post types that use the same default WP category.
Is it possible to add a meta_query that checks if the category is used in a Custom post_type? eq: custom post type: work.
$work_categorys = get_terms(
[
'taxonomy' => "category",
'hide_empty' => true,
]
);
foreach ($work_categorys as $key => $value) { echo '<li data-filter-tag="'.$value->slug.'" class="">'.$value->name.'</li>'; }
I'm trying to get a list of categories if they are used in a Custom Post type. The post type uses the default categories taxonomy. There are also other custom post types that use the same default WP category.
Is it possible to add a meta_query that checks if the category is used in a Custom post_type? eq: custom post type: work.
$work_categorys = get_terms(
[
'taxonomy' => "category",
'hide_empty' => true,
]
);
foreach ($work_categorys as $key => $value) { echo '<li data-filter-tag="'.$value->slug.'" class="">'.$value->name.'</li>'; }
Share
Improve this question
asked Jan 29, 2020 at 9:45
BonttimoBonttimo
1932 silver badges5 bronze badges
2 Answers
Reset to default 2This would work. As posted by @bucketpress.
$someposts = get_posts(
array(
'post_type' => 'work',
'posts_per_page' => -1,
'fields' => 'ids', // return an array of ids
)
);
$somepoststerms = get_terms(
array(
'taxonomy' => 'category',
'object_ids' => $someposts,
'hide_empty' => true,
)
);
To get all terms from all taxonomies assigned to "work" custom post type you can customize your code to look like this
$taxes = array_keys( get_object_taxonomies( 'work', 'objects' ) );
$work_categorys = get_terms(
[
'taxonomy' => $taxes,
'hide_empty' => true,
]
);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744789277a4593816.html
评论列表(0条)