You’ve to take a look at the documentation to know how to use a function, you can’t just replace them. If you do that you’ll see get_post_types()
has to be used different then get_categories()
. For example you can’t use the parameter hide_empty
. Besides that you have to change the $output
parameter to objects
, default is names
. There are many other possibilities for customizing your output, if you make use of the $args
and $operator
parameter. A basic untested example based on what you’ve tried so far is below.
Code:
$args = array(
// public - Boolean. If true, only public post types will be returned.
'public' => true,
// _builtin - Boolean.
// If true, will return WordPress default post types.
// Use false to return only custom post types.
'_builtin' => false
);
$output="objects";
$operator="and";
$cpt_post_types_obj = get_post_types( $args, $output, $operator );
$cpt_post_types = array();
foreach ( $cpt_post_types_obj as $cpt_apt ) {
$cpt_post_types[] = $cpt_apt->name;
}
$post_types_tmp = array_unshift($cpt_post_types, "Select a post type:");
Update:
As the constructing of the custom post type array works with above code, the problem might have to do with your dropdown. Below an example how you can do it.
Code:
echo '<select name="post_type_dropdown">';
foreach ( $cpt_post_types_obj as $cpt_apt ) {
echo '<option value="'. $cpt_apt->name .'">'. $cpt_apt->name . '</option>';
}
echo '</select>';
2nd Update:
Like I said, it’s working for me. Use below code, just put it into your functions.php
to test, debug it.
Code:
add_action( 'init', 'wpse132165_pt_list_debug' );
function wpse132165_pt_list_debug() {
$args = array(
// public - Boolean. If true, only public post types will be returned.
'public' => true,
// _builtin - Boolean.
// If true, will return WordPress default post types.
// Use false to return only custom post types.
'_builtin' => false
);
$output="objects";
$operator="and";
$cpt_post_types_obj = get_post_types( $args, $output, $operator );
// open up the next line like this /** to comment the code until the next /**/
/**/
echo '<pre>';
print_r($cpt_post_types_obj);
echo '</pre>';
/**/
$cpt_post_types = array();
foreach ($cpt_post_types_obj as $cpt_apt) {
$cpt_post_types[] = $cpt_apt->name;
}
/**/
echo '<pre>';
print_r($cpt_post_types);
echo '</pre>';
/**/
echo '<select name="post_type_dropdown">';
foreach ( $cpt_post_types_obj as $cpt_apt ) {
echo '<option value="'. $cpt_apt->name .'">'. $cpt_apt->labels->name . '</option>';
}
echo '</select>';
}