Skip Navigation

[Resolved] Using get_post_types does not return custom post types

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to get, programmatically, the custom post types defined by toolset at the init action.

Solution:
Toolset types and API is defined before the priority 20. In order to use them, you need to use a higher priority:

add_action('init', 'cpt_archive_acf_pages', 100);

Relevant Documentation:

This support ticket is created 2 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by Aldo 2 years, 4 months ago.

Assisted by: Jamal.

Author
Posts
#2240629

Hello

We try to add an ACF options page to each CPT.
For this we want to fetch all registered CPT.

But using get_post_types does not return the registered custom post types. Only build in or the ones crated by WP types, but the ones created WITH WP Types.

For testing purposes we use this code:

function cpt_archive_acf_pages() {

// Fetch registered CPT
$args = array(
'_builtin' => false
);
$output = 'objects'; // 'names' or 'objects' (default: 'names')
$post_types = get_post_types( $args, $output );
print '<pre>';
print_r($post_types);
print '</pre>';
die();

}

add_action('init', 'cpt_archive_acf_pages');

What are we missing?

Thanks
Aldo

#2240687

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello Aldo and thank you for contacting the Toolset support.

The default prioirty for the add_action function is 10. At this stage, Toolset did not register its post types and loaded its API yet. Try to register this action at a higher prioirty, let's say 100.
- https://developer.wordpress.org/reference/functions/add_action/
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#snippet-execution-timing

Try this for the last line:

add_action('init', 'cpt_archive_acf_pages', 100);

Let me know what you will get.

#2240697

Perfect. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.