As Milo hints at in their comment, custom post types can only be registered on the init
hook. Therefore, if you are trying to get all the public but not builtin post types before init
, then get_post_types()
will return an empty array. (You can use get_post_types()
to get the builtin post types before init
.)
What you need to do is hook in after init
and then get the post types. I’d use wp_loaded
since at this point, WordPress is fully loaded.
add_action( 'wp_loaded', 'wpse290170_wp_loaded' );
function wpse290170_wp_loaded() {
$types = get_post_types( [
'public' => true,
'_builtin' => false
] );
foreach ( $types as $type ){
add_action( "publish_" . $type, 'duplicate_title_buwp_bc' ) ;
}
}
Related Posts:
- Passing a parameter to filter and action functions
- Get a list of all registered actions
- Where is the best place to use add_filter
- Is it possible to define a template for a custom post type within a plugin independent of the active theme?
- Deactivate plugin for a specific user group
- Ways to have multiple front-page.php templates that can be swapped out?
- Removing an action from an external plugin class
- When can you get current page ID and initialize hooks right after?
- How can I limit functionality in one version of a plugin?
- Which hook callback has priority if both plugin and theme use the same hook?
- Use external link in the add sub menu
- Template for custom post type shows all posts instead of just one
- delay function on publish?
- How to prepend to the_title for admin-side plugin’s use
- How can I enqueue a style only when a particular widget is active?
- Add custom buttons with custom actions in Edit Post screen in WordPress?
- Can a plugin add to header/footer/body content?
- How to filter content post only on save
- Filter or action hook to insert content on admin pages before tag
- WordPress Plugin Boilerplate – add_action hook in static “activate” function
- How to call bind function in wordpress actions or hooks
- template_redirect not being called when using ajax
- Can’t get custom posts of taxonomy to show
- Plugin custom Action Hook not working
- add_action in functions.php, do_action in plugin?
- add action for displaying posts using a shortcode
- ‘wp_login’ action hook not working with wp-login.php file
- Display content from custom post type if match to main post category
- How to Use Parameters with a Do_Action Function Within PHP Tags
- WordPress filter that hook after each action/filter hook
- Override global query results without hooks
- Automatically add attributes to woocommerce product?
- Calling plugin function inside custom plugin for onclick event
- CampaignMonitor for WooCommerce – Move subscribe button [closed]
- Plugin developer automated documentation
- Custom plugin init action causing general slowness
- Is there a hook to Intercept al urls from a webpage and redirect to a page
- Inject HTML meta tag inside wordpress tag using add_shortcode
- How to get post ID with hooks publish_post, new_to_publish, etc
- register_activation_hook in oop approach
- do add_action on condition
- Is there a way to figure out which action/filter invoked a function?
- How to trace SUBMIT button
- Remove_action from plugin with a Class
- Get User_id outside wordpress,
- publish_post action doesn’t work
- Import Instagram post to WordPress blog post
- the_post hook is not firing for me
- do_action Nested List
- Where to find the code used to render a page that has a shortcode and a template defined?
- Plugin Development – Functions or Hooks?
- Basic function call on init failing
- How to create Admin Notice from Plugin with argument? [duplicate]
- Editing wp-config.php
- Are there action hooks for comments?
- New bulk action to resend welcome emails
- Loop in elementor custom widget not working
- get_users(); Is an Undefined Function When Used in Cron Job
- WooCommerce: after install hook
- Loop returns the current page’s permalink and guid instead of the post in the loop
- Action on WordPress Install
- Retrieve options set through a plugin
- override function from my plugin [closed]
- What hook can I use to modify custom post data before it is displayed on the page?
- Output after specific post in the loop
- Plugin is creating posts twice
- Running a function with args in add_action()
- Multiple images per post – Show random image elsewhere
- How to append new form elements in “Add New” form of Users in WordPress admin panel?
- Displaying page content from plugin, inside exising empty WP page
- Hook from plugin doesn’t fire up from external PHP script
- Callback hooked to post_updated firing on new posts as well
- The function called on the wp head hook becomes null
- Discern a specific plugin’s action hooks
- Can’t add new plugin or themes on a wordpress multisite network from the primary site as administrator. Error: ERR_TOO_MANY_REDIRECTS
- How to remove default action from a plugin?
- How to Unhook actions/filters in within Class in plugin
- WPML – Hook when language is switched (change user language)
- Nested Actions and Filters
- Use action, filter, or hook to append HTML to WordPress plugin function
- Passing function into add_action always returns the first argument
- Change hook to add_action in a plugin class
- echo plugin results on pages
- How to get bulk actions handler to display simple “Hello World”?
- What action or filter can I use to change all the html returned from server for a page?
- What action/hook do I need to register to have my plugin handle front-end editing?
- Create post loop that displays pages by cat ID
- wp_login_form() ignoring login_form action hook
- how to repeat taxonomy in different places on wordpress
- Removing this filter added by a plugin
- ‘all’ hook and add_action issue (class based plugin)
- Which filters or actions involve with index page for plugin?
- single.php fires more than once after clicking on any post to view with different post id each time
- How can I save a setting field with multiple checkbox options generated by a foreach loop on a custom wordpress admin page?
- Call a function with href
- WooCommerce Order Status Displays Twice on Order Page
- Outputting custom field query from a plugin to the website header
- Which action/filter can i use for a Member Plugin [closed]
- My wp_update_nav_menu action is firing twice
- FacetWP custom display based on post type [closed]