Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

list registered post_types that are built in and public WordPress

  • SOLVED

I am trying to populate a select box with an array of registered post_types that are both public and not _builtin

Here is an example of how I am retreiving a list of categories, which I wish to 'duplicate' and use to retreive the post_types


//Access the WordPress Categories via an Array
$woo_categories = array();
$woo_categories_obj = get_categories('hide_empty=0');
foreach ($woo_categories_obj as $woo_cat) {
$woo_categories[$woo_cat->cat_ID] = $woo_cat->cat_name;}
$categories_tmp = array_unshift($woo_categories, "All Categories:");


I am able to call this in my theme admin panel with the following:

$options[] = array( "name" => "Post Category",
"desc" => "Select the category of articles to display",
"id" => $shortname."_panel_1_category",
"std" => "",
"type" => "select",
"class" => "panel_1_category",
"options" => $woo_categories);


Here is where I was going with this, only it is obviously incomplete

//Access the WordPress Custom Post types via an Array
$woo_post_types = array();
$woo_post_types_obj = get_post_types('public=true,_builtin=false');
foreach ($woo_post_types_obj as $woo_post_type) {
*****stuff needs to go here*****
}
$woo_post_types_tmp = array_unshift($woo_post_types, "select a post type:");


I would call it like:

$options[] = array( "name" => "Post Type",
"desc" => "Select the post type to display",
"id" => $shortname."_panel_1_post_type",
"std" => "",
"type" => "select",
"class" => "panel_1_post_type",
"options" => $woo_post_types);


The problem is $woo_post_types select box is not being populated with my registered post_types as my function to retrieve them above is not complete.

I need $woo_post_types to return the array of post_types that are public and _builtin

thanks

Answers (3)

2010-10-22

rilwis answers:

This works:

$custom_post_types = array();
$custom_post_type_objs = get_post_types(array('_builtin' => false), 'objects');
foreach ($custom_post_type_objs as $custom_post_type_obj) {
$custom_post_types[] = $custom_post_type_obj->label; // you can use $custom_post_type_obj->singular_label as well
}


shawn comments:

Thanks Rilwis

Still stuck.. .works outside of the woo admin panel, but not inside it. I get a blank array every time. Tried everyone's snippets and none work with woo. Kinda stuck after all day trying

2010-10-22

Utkarsh Kukreti answers:

//Access the WordPress Custom Post types via an Array

$woo_post_types = array();

$woo_post_types_obj = get_post_types('public=true,_builtin=false', 'objects');

foreach ($woo_post_types_obj as $woo_post_type) {

$woo_post_types[$woo_post_type->name] = $woo_post_type->label;

}

$woo_post_types_tmp = array_unshift($woo_post_types, "select a post type:");


shawn comments:

any chance you have tried this within the woo admin panel?

I am able to get it to work, along with similar functions outside the theme admin panel, but have no results when using it within the panel.

2010-10-22

Denzel Chia answers:

Hi,

You are using woo themes right?

Please use the following codes.

$post_types=get_post_types(array('public'=>true,'_builtin' => true),'names');
$wp_cats = array();
foreach ($post_types as $post_type ) {
$wp_cats[] = $post_type;
}
array_unshift($wp_cats, "Choose a Post Type");

$options = array (

array( "name" => "Post Category",

"desc" => "Select the post type",

"id" => $shortname."_panel_1_category",

"std" => "",

"type" => "select",

"class" => "panel_1_category",

"options" => $wp_cats),


);

The above outputs all builtin post types as a select list.
Please attached screenshot as prove that this script works.

However I am not able to get the custom post types to list out.
If I change '_builtin' to false, it returns nothing.

Thanks.


shawn comments:

Was in a bit of a rush this morning, paid before checking any of the examples...

You are correct, when I change _builtin to false, I get nothing returned. Of course turning it to true returns the wp builtin post types like your example shows.

What I don't understand:
When I run the following function, I am given a list of my registered post-types that I created myself. This shows me that the post-types are registered correctly.


<?php
$args=array(
'name' => 'property'
);
$output = 'objects'; // names or objects
$post_types=get_post_types($args,$output);
foreach ($post_types as $post_type ) {
echo '<p>' . $post_type->name . '</p>';
}
?>


I have tried all 3 script examples on this question, and when used within woo, none of them will return the builtin post types.

Any ideas what may be happening, like woo somehow messing things up?

--just like your example I added the functions to the theme-options.php file.


shawn comments:

sorry pasted in wrong code above....


<?php
$args=array(
'public' => true,
'_builtin' => false
);
$output = 'names'; // names or objects
$operator = 'and'; // 'and' or 'or'
$post_types=get_post_types($args,$output,$operator);
foreach ($post_types as $post_type ) {
echo '<p>'. $post_type. '</p>';
}
?>


shawn comments:

by any chance did you have any luck figuring out why it returns a blank array using _builtin=>false ?

Seems I should have tried the functions prior to closing the question, as no matter what I try, I cannot get back my post types.

I have no problem running a similar function outside the woo panel and receiving the expected array of post types.

something is very screwy with the way woo is doing this, been on this all day still with no luck at all.