如何在WordPress中检索post_type的查询列表?

时间:2019-01-10 07:06:21

标签: wordpress

我正在开发一个插件,需要WordPress后端中存在的帖子类型列表,我检查了WordPress Codex的WP_Query页面,但找不到解决方案

1 个答案:

答案 0 :(得分:2)

您可以尝试使用此代码。

<?php
// hook into init late, so everything is registered
// you can also use get_post_types where ever.  Any time after init is usually 
fine.
add_action( 'init', 'wpse34410_init', 0, 99 );
function wpse34410_init() 
{
$types = get_post_types( [], 'objects' );
foreach ( $types as $type ) {
    if ( isset( $type->rewrite->slug ) ) {
        // you'll probably want to do something else.
        echo $type->rewrite->slug;
    }
}
}

我认为它将为您服务