Any way to have save_post for custom posts only? The way my functions.php is coded is tacking on lots of custom fields to normal posts and pages who don't need/use them.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Updated since 3.7.0 - props @Baptiste for the reminder
3.7.0 introduced the "
save_post_{$post->post_type}
" hook, which will be triggered by the post type. This allows you to add an action specific to your custom post type (or "page" or "post" etc). This saves you one line of the below.The accepted method is to add an action on
save_post_{post-type}
(substituting your post type's slug for{post-type}
in the example above). There are a number of checks you can / probably should still do within your action's callback, which I document in the example below:from the Codex:
If you are registering multiple custom post types and you would like to consolidate your save_post functionality into a single function, then hook on the generic
save_post
action. But then remember to do your post type check within your function if there is any differences in how those post types save their data.eg:
if ( 'my-cpt-1' == $post->post_type ){ // handle my-cpt-1 specific stuff here ...
WordPress 3.7 introduced a new way of handling this with the
save_post_{$post_type}
hook.Let's say your custom post type is "member-directory". You can now run save_post on that post type only by using something like this: