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
- How can I edit post data before it is saved?
- Manually set global $post vars with an ID, in order to use template tags
- How can I see all the actions attached to an “add_action” hook?
- 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
- Auto Load Plugin Hooks inside Must Use Plugin
- How to only hook on Single.php after content?
- Ways to have multiple front-page.php templates that can be swapped out?
- Output before and after the loop
- Removing an action from an external plugin class
- When can you get current page ID and initialize hooks right after?
- How can I log a user out of WordPress before the page loads?
- What is the ‘admin_action_’ . $_REQUEST[‘action’] hook used for?
- Define Function in functions.php or plugin and call in theme loop
- Autogenerate wordpress shortcodes using array?
- WordPress admin notice in plugin function
- Using the ‘draft_to_publish’ hook (post status transition)
- Enqueue style inside shortcode but its loaded at the bottom of page (before footer scripts)
- How can I limit functionality in one version of a plugin?
- Is there a global action for when a plugin is uninstalled?
- Shortcode display outside the div
- Which hook callback has priority if both plugin and theme use the same hook?
- Good tools for locating hooks in a wordpress page/admin interface/blog post?
- How can I let users to access plugin functions based on roles?
- Use external link in the add sub menu
- Template for custom post type shows all posts instead of just one
- How to remove action from 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
- Get post content inside plugin class method
- Ajax call to php function doesn’t work PHP code
- Remove action added in plugin class from theme
- {status}_{post_type} does not run correctly?
- Change hook to add_action in a plugin class
- Frontend AJAX Request causes Error: ‘Call to undefined function add_action’
- echo plugin results on pages
- Creating Admin Submenu Page via Class Method
- How to get bulk actions handler to display simple “Hello World”?
- Help needed with woocommerce (wc stripe) filter
- Why is WP template_include overwritting all templates rather than specified page?
- 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
- Why can’t I shove an instance of a class into a variable from a do_action hook?
- wp_login_form() ignoring login_form action hook
- Place content inside the Post Loop
- Why doesn’t a form need an ‘action’ with a plugin that uses the post data?
- Passing a parameter to filter and action functions
- Run only on plug-in activation instead of wp_head
- how to repeat taxonomy in different places on wordpress
- Am I using an action hook correctly?
- plugins_loaded action is not working properly
- AJAX login without a plugin does not work. when add a action to function.php
- Cannot reset a loop in a plugin template
- Remove action working on functions.php but not in a plugin. Why?
- Plugin Hook: Get posts
- Displaying image from a repeatable group
- Action hook “wp_insert_post” works but not for last imported post
- add_media_page function not creating submenu
- change output location of plugin function using a custom hook
- Multiselect value in wp_query
- Remove action plugin
- Removing this filter added by a plugin
- Advanced Custom Fields (ACF) Plugin – Random Image in Sidebar
- Filter for modifying image on upload
- ‘all’ hook and add_action issue (class based plugin)
- Fake Single WordPress Post (Page) Loop
- Is it possible to cancel a post status transition?
- Which filters or actions involve with index page for plugin?
- Problem with executing a function on saving a post
- single.php fires more than once after clicking on any post to view with different post id each time
- OOP Plugin: Where should I place the action hooks in the class?
- How do i use postMash to order posts in a custom loop?
- add query string to all pages after user logged in
- How can I save a setting field with multiple checkbox options generated by a foreach loop on a custom wordpress admin page?
- How wordpress plugin hooks works? [duplicate]
- Getting Post Permalink Outside of Loop Not Working
- Trying to display posts but getting the pages as output
- Call a function with href
- WooCommerce Order Status Displays Twice on Order Page
- How to dispaly post informations (such as titles) in an admin plugin menu page?
- Outputting custom field query from a plugin to the website header
- Which action/filter can i use for a Member Plugin [closed]
- Hook automatic_updates_complete to autoupdate plugin
- My wp_update_nav_menu action is firing twice
- How to display content depending on the Woocommerce Product Category
- Page with redirect
- FacetWP custom display based on post type [closed]
- add_action() not working for admin
- WordPress Action Hook inside Classes
- wp_head filter not executed inside custom class
- Add 2 links in between the navigation using the Breadcrumb Navxt plugin hook
- Add custom button to the changeset status in the Customizer
- Remove 3rd party plugin notices from within own plugin
- How to customize WP_Error (REST JWT authentication plugin) [closed]