0

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())
            ));
    }
}
3
  • I deleted my answer (for now), and when you said, "still not working", did you actually mean that your function returns nothing when you call it from within the lnpa_attach_theme_options() function? Try doing var_dump( $this->test_get_post_types() ); or error_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 making get_post_types() returns the proper post types, then that's an off-topic question here.
    – Sally CJ
    Mar 9 at 17:06
  • It returns only 'post', 'page', and 'attachment' post types. Mar 11 at 0:11
  • So this was actually an issue with Carbon Fields, nonetheless, see my new answer and let me know. Also, remember to remove that add_action('init', array($this, 'test_get_post_types'), 999); from your code.
    – Sally CJ
    Mar 11 at 4:02

1 Answer 1

0

You are not seeing or that your function (test_get_post_types) is not returning the custom post types because if you look at the Carbon Fields source code, carbon_fields_register_fields is run by Carbon_Fields\Loader\trigger_fields_register() which is hooked on init with the priority 0. (See lines 113 and 44 in carbon-fields/core/Loader/Loader.php)

So that means, by the time your lnpa_attach_theme_options function runs (i.e. called via the carbon_fields_register_fields hook), those custom post types ("book", "movie", etc.) have not yet been registered because despite custom post types are commonly registered during init, the priority would not be 0 in normal cases.

Therefore, instead of passing the output of your test_get_post_types function, you should pass the function as a callable to the add_options() function, like so:

Field::make('select', 'ggg_post_type', __('Select post type.'))
    // Pass a callable, i.e. a function that is called later.
    ->add_options( array( $this, 'test_get_post_types' ) )

See the Carbon Fields documentation for more/other information.

1
  • 1
    Thank you, working perfectly. Mar 11 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.