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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Updated since 3.7.0 – props @Baptiste for the reminder
Updated to include new Dev doc reference – props @stephendwolff
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: (updated: Dev Reference)
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_postaction. 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 ...