admin管理员组文章数量:1330645
I'm trying to get all the post types that has tags functionality. I searched a lot but couldn't find a conditional or something for that. I need a way to get post types or post objects for the posts which are taggable.
Here are my current code to get post types:
public function getPostTypes() {
$excludes = array('attachment');
$postTypes = get_post_types(
array(
'public' => true,
),
'names'
);
foreach ($excludes as $exclude) {
unset($postTypes[$exclude]);
}
return array_values($postTypes);
}
Thanks!
I'm trying to get all the post types that has tags functionality. I searched a lot but couldn't find a conditional or something for that. I need a way to get post types or post objects for the posts which are taggable.
Here are my current code to get post types:
public function getPostTypes() {
$excludes = array('attachment');
$postTypes = get_post_types(
array(
'public' => true,
),
'names'
);
foreach ($excludes as $exclude) {
unset($postTypes[$exclude]);
}
return array_values($postTypes);
}
Thanks!
Share Improve this question asked Jul 28, 2020 at 10:05 DaftPlugDaftPlug 31 silver badge4 bronze badges2 Answers
Reset to default 0Please try this.
function get_post_tp(){
$args = array(
'public' => true,
'_builtin' => false,
);
$post_types = get_post_types( $args, 'objects' );
foreach ( $post_types as $key => $value ) {
$postArray = json_decode(json_encode($post_types), true);
$array_key = key($postArray);
print_r($post_types[$array_key]->taxonomies); // here you can see in taxonomies post_tag is exist or not.
//if exist then push into array otherwise you can skip it..
//apply your logic here
}
}
add_action('init','get_post_tp');
Solution is to check for post_tag
taxonomy existence.
public function getPostTypes() {
$excludes = array('attachment');
$getPostTypes = array_values(
get_post_types(
array(
'public' => true,
),
'names'
)
);
foreach ($excludes as $exclude) {
unset($getPostTypes[$exclude]);
}
$postTypes = array();
foreach ($getPostTypes as $postType) {
get_object_taxonomies($postType);
if (in_array('post_tag', get_object_taxonomies($postType))) {
$postTypes[] = $postType;
}
}
return $postTypes;
}
本文标签: Get all post types that supports tags
版权声明:本文标题:Get all post types that supports tags 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742226944a2436496.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论