This question already has answers here: Get list of registered custom post types (5 answers) Closed 6 years ago.admin管理员组文章数量:1130349
I'm trying to create a dropdown-list of all my custom post types.
It would look like this:
<select>
<option value="post">Post</option>
<option value="book">Book</option>
<option value="some-other-post-type">Something</option>
<option value="team">Team Members</option>
</select>
I have come across the get_post_types() function which is supposed to get a list of all registered post type objects. But it doesn't show my custom post types...
Is it possible to get an array with the slug and the title of all the post types registered in a theme? Considering the number of post types is unknown and dynamic. Every time a new custom post type is added or removed, it should reflect in the dropdown list.
My test:
$args = array(
'public' => true,
'_builtin' => false // Use false to return only custom post types
);
$post_types = get_post_types( $args );
print_r($post_types);
It returns an empty array... no custom post types that I registered.
This question already has answers here: Get list of registered custom post types (5 answers) Closed 6 years ago.I'm trying to create a dropdown-list of all my custom post types.
It would look like this:
<select>
<option value="post">Post</option>
<option value="book">Book</option>
<option value="some-other-post-type">Something</option>
<option value="team">Team Members</option>
</select>
I have come across the get_post_types() function which is supposed to get a list of all registered post type objects. But it doesn't show my custom post types...
Is it possible to get an array with the slug and the title of all the post types registered in a theme? Considering the number of post types is unknown and dynamic. Every time a new custom post type is added or removed, it should reflect in the dropdown list.
My test:
$args = array(
'public' => true,
'_builtin' => false // Use false to return only custom post types
);
$post_types = get_post_types( $args );
print_r($post_types);
It returns an empty array... no custom post types that I registered.
Share Improve this question edited Dec 23, 2018 at 12:45 at least three characters asked Dec 23, 2018 at 11:22 at least three charactersat least three characters 33712 silver badges28 bronze badges 15 | Show 10 more comments1 Answer
Reset to default 2OK, so the function get_post_types is exactly what you're looking for.
$args = array(
'public' => true,
'_builtin' => false
);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types( $args, $output, $operator );
foreach ( $post_types as $post_type ) {
echo '<p>' . $post_type . '</p>';
}
But there are few things you should be careful about:
- You can't get these post types too soon. It's very common practice to register the post types on
inithook, so you won't get these post types before that hook is fired up. - If you'll query for public post types, you'll get only the post types that are registered as public (and not that are public based on other args)...
- By default this function will return you only names of post types. If you want to obtain more info, you can pass
objectsas second param.
I'm trying to create a dropdown-list of all my custom post types.
It would look like this:
<select>
<option value="post">Post</option>
<option value="book">Book</option>
<option value="some-other-post-type">Something</option>
<option value="team">Team Members</option>
</select>
I have come across the get_post_types() function which is supposed to get a list of all registered post type objects. But it doesn't show my custom post types...
Is it possible to get an array with the slug and the title of all the post types registered in a theme? Considering the number of post types is unknown and dynamic. Every time a new custom post type is added or removed, it should reflect in the dropdown list.
My test:
$args = array(
'public' => true,
'_builtin' => false // Use false to return only custom post types
);
$post_types = get_post_types( $args );
print_r($post_types);
It returns an empty array... no custom post types that I registered.
This question already has answers here: Get list of registered custom post types (5 answers) Closed 6 years ago.I'm trying to create a dropdown-list of all my custom post types.
It would look like this:
<select>
<option value="post">Post</option>
<option value="book">Book</option>
<option value="some-other-post-type">Something</option>
<option value="team">Team Members</option>
</select>
I have come across the get_post_types() function which is supposed to get a list of all registered post type objects. But it doesn't show my custom post types...
Is it possible to get an array with the slug and the title of all the post types registered in a theme? Considering the number of post types is unknown and dynamic. Every time a new custom post type is added or removed, it should reflect in the dropdown list.
My test:
$args = array(
'public' => true,
'_builtin' => false // Use false to return only custom post types
);
$post_types = get_post_types( $args );
print_r($post_types);
It returns an empty array... no custom post types that I registered.
Share Improve this question edited Dec 23, 2018 at 12:45 at least three characters asked Dec 23, 2018 at 11:22 at least three charactersat least three characters 33712 silver badges28 bronze badges 15- How do you use that function? Show your current code, pls. – Krzysiek Dróżdż Commented Dec 23, 2018 at 11:44
-
Just
get_post_types()and it returns an object but no custom post types included. – at least three characters Commented Dec 23, 2018 at 12:12 - But where do you use it? How do you use it? – Krzysiek Dróżdż Commented Dec 23, 2018 at 12:12
- I used it in functions.php. I am just testing it to see what it returns. I'm trying to find a starting point to build the actual function. i'm not sure where to start to get the post type names – at least three characters Commented Dec 23, 2018 at 12:17
- it all depends on context... Seriously, show us your code, or we won’t be able to help you... It’s hard to guess what’s wrong with code that we can’t see... – Krzysiek Dróżdż Commented Dec 23, 2018 at 12:20
1 Answer
Reset to default 2OK, so the function get_post_types is exactly what you're looking for.
$args = array(
'public' => true,
'_builtin' => false
);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types( $args, $output, $operator );
foreach ( $post_types as $post_type ) {
echo '<p>' . $post_type . '</p>';
}
But there are few things you should be careful about:
- You can't get these post types too soon. It's very common practice to register the post types on
inithook, so you won't get these post types before that hook is fired up. - If you'll query for public post types, you'll get only the post types that are registered as public (and not that are public based on other args)...
- By default this function will return you only names of post types. If you want to obtain more info, you can pass
objectsas second param.
本文标签: Get a list of all custom post type namesslugs
版权声明:本文标题:Get a list of all custom post type namesslugs 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749073336a2311921.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


get_post_types()and it returns an object but no custom post types included. – at least three characters Commented Dec 23, 2018 at 12:12