Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6011619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:17:11+00:00 2026-05-23T02:17:11+00:00

Any way to have save_post for custom posts only? The way my functions.php is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:17:11+00:00Added an answer on May 23, 2026 at 2:17 am

    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)

    /* Register a hook to fire only when the "my-cpt-slug" post type is saved */
    add_action( 'save_post_my-cpt-slug', 'myplugin_save_postdata', 10, 3 );
    
    /* When a specific post type's post is saved, saves our custom data
     * @param int     $post_ID Post ID.
     * @param WP_Post $post    Post object.
     * @param bool    $update  Whether this is an existing post being updated or not.
    */
    function myplugin_save_postdata( $post_id, $post, $update ) {
      // verify if this is an auto save routine. 
      // If it is our form has not been submitted, so we dont want to do anything
      if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
          return;
    
      // verify this came from the our screen and with proper authorization,
      // because save_post can be triggered at other times
    
      if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
          return;
    
      
      // Check permissions
      if ( 'page' == $post->post_type ) 
      {
        if ( !current_user_can( 'edit_page', $post_id ) )
            return;
      }
      else
      {
        if ( !current_user_can( 'edit_post', $post_id ) )
            return;
      }
    
      // OK, we're authenticated: we need to find and save the data
    
      $mydata = $_POST['myplugin_new_field'];
    
      // Do something with $mydata 
      // probably using add_post_meta(), update_post_meta(), or 
      // a custom table (see Further Reading section below)
    
       return $mydata;
    }
    

    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 ...

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to have PHP automatically call a function, before a script
Is there any way to have something that looks just like a file on
Is there any way to have a Windows batch file directly input SQL statements
Is there any way to have a dropdown in excel that will show a
Is there any way to have the QCompleter to act like an autocomplete for
Is there any way to have a label wordwrap text as needed? I have
Is there any way to have to tabcontrol take the size of the largest
Is there any way to have a default MessageBox.Show() caption? Let says I would
Is thre any way to have an enum entry with a hyphen, -, in
Is there any way to have a template inherit another template? I'm not using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.