0

I have this code in a page of my custom theme:

$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 ); 

This code if loaded in a wp-admin page.

The problem is that I want to load all types of post, I tried changing the built in but it does not work. I want to get the default post of wordpress and the custom post type as woocomerce products. Does someone help me? Thank you!

2 Answers 2

2

Custom post types can only be registered on the init hook. So if you're trying to get the post types before the init hook, you will only ever get the built-in ones.

To get custom post types, you need to use a hook after init, or later on init than the custom post types were registered.

function wpse_func() {
  $args = array(
    'public'   => true,
    '_builtin' => false,
  ];
  $post_types = get_post_types( $args );
}
add_action( 'init', 'wpse_func', PHP_INT_MAX );
//* Or
add_action( 'wp_loaded', 'wpse_func' );
2
  • Well, but your code will get only public and not builtin types. And OP wants to get all registered types ;) Apr 4, 2018 at 19:35
  • Right, this is what I had thought. The problem is that I was loading directly without using the hook wp_loaded then I did not take the custom_post. Thank you! Apr 4, 2018 at 21:24
1

First of all, you don't need to create variables just to pass them as arguments when calling a function.

And getting back to your problem... You pass 'AND' as last argument, so the conditions will be joined with 'AND' - so you will get only post types that are public AND _builtin.

If you want to get all public OR _builtin post types, then you should use OR operator:

$post_types = get_post_types( array('public' => true, '_builtin' => true), 'names', 'or' ); 

But if you want to get all registered post types (not all of them have to be public), then this code will help (you don't wont any conditions in there):

$post_types = get_post_types( array(), 'names' ); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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