I am building a WordPress plugin and I want to show all publicly registered custom post types (by theme or other plugins) in my plugin. But it only shows post and page custom post types not other publicly registered custom post types like book, movie, and so on.
My code:
class MyClass
{
// Boot carbon fields plugin
public function lnpa_crb_load()
{
Carbon_Fields::boot();
}
// Register carbon fields and init submenu page
public function __construct()
{
add_action('carbon_fields_register_fields', array($this, 'lnpa_attach_theme_options'));
add_action('after_setup_theme', array($this, 'lnpa_crb_load'));
add_action('init', array($this, 'test_get_post_types'), 999);
}
public function test_get_post_types()
{
$post_types_arr = array();
$args = array(
'public' => true,
// '_builtin' => true
);
// $post_types = get_post_types($args, 'names', 'and');
// $post_types = array_diff($post_types, array('page', 'attachment'));
$output = 'objects'; // Return post type objects
$operator = 'and';
$post_types = get_post_types($args, $output, $operator);
foreach ($post_types as $post_type) {
$post_types_arr[$post_type->name] = ucfirst($post_type->name);
}
return $post_types_arr;
}
// Declare carbon fields options
public function lnpa_attach_theme_options()
{
Container::make('theme_options', __('AAA Settings', 'text_domain'))
->add_fields(array(
Field::make('select', 'ggg_post_type', __('Select post type.'))
->add_options($this->test_get_post_types())
));
}
}
lnpa_attach_theme_options()
function? Try doingvar_dump( $this->test_get_post_types() );
orerror_log( var_export( array( __METHOD__, $this->test_get_post_types() ), true ) );
in that function and see what the dump says? But note that, if you're having an issue making it work with a 3rd-party plugin, rather than an issue makingget_post_types()
returns the proper post types, then that's an off-topic question here.add_action('init', array($this, 'test_get_post_types'), 999);
from your code.