1

I have a custom post type "soto_user". In which I have custom meta field named "Publish" from which user can select 'YES' or 'No'. If user selects "Yes" and click Publish button then post should be published or if user selects "No" and click Publish button then post should be saved as DRAFT.

function sotoUserManagement( $post )   // function that echo custom meta field in CPT
  {
    $values = get_post_custom( $post->ID );
    $post_status=get_post_status($post->ID);
    $publish='';
    if($post_status=='publish'){$publish=1;}
    if($post_status=='draft'){$publish=0;}
?>
      <table id="sotoUserManagementTable" class="sotoUserTable">
       <tr>
         <td>
             <label for="publish">Publish</label>
             <select name="publish" id="publish">
               <option value="1" <?php selected( $publish, 1 ); ?>>Yes</option>
               <option value="0" <?php selected( $publish, 0 ); ?>>No</option>
             </select>
         </td>
         <td>
         </td>
       </tr>
      </table>
<?php
  }  


add_action( 'save_post', 'soto_user_meta_box_save' ); //calls on Click Publish Button
 function soto_user_meta_box_save( $post_id )
 {

     // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn't there, or we can't verify it, bail
   // if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;

    // Make sure your data is set before trying to save it

     if($_POST['publish'] == 1 && get_post_status($post_id) != 'publish' && get_post_type( $post_id ) == 'soto_user'){ //whatever your post variable is
        remove_action('save_post', 'soto_user_meta_box_save'); //if you don't unhook the function you'll have an infinite loop
        wp_publish_post($post_id);
        add_action('save_post', 'soto_user_meta_box_save'); //rehook the function       
    }
    if($_POST['publish'] == 0 && get_post_status($post_id) == 'publish' && get_post_type( $post_id ) == 'soto_user'){
        remove_action('save_post', 'soto_user_meta_box_save'); //if you don't unhook the function you'll have an infinite loop
        wp_update_post(array(
            'ID' => $post_id,
            'post_status' => 'draft'
        ));
        add_action('save_post', 'soto_user_meta_box_save'); //rehook the function
    }
}

From above code if I save my custom post then it works means it saves post as PUBLISH if custom field "Publish" set values "YES" and it saves post as DRAFT when custom field "Publish" is choosen "NO".

Main problem that I am getting on saving Pages. If I create a new page and then save it then it never saved as "PUBLISH", it always saved as "DRAFT". And now if I remove above code then pages seems to be working.

Why above code is affecting the Page section.? or is there another code to accomplish my need ?

0

The problem lies in your soto_user_meta_box_save function. This function is tied to the save_post action, which means it will fire whenever any post of any post type is saved, not just your custom post type.

You need to add a check inside this function so it aborts when testing the wrong post type:

function soto_user_meta_box_save( $post_id ) {

    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn't there, or we can't verify it, bail
    // if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;

    // if this isn't the right post type, bail
    if ( 'soto_post_type' !== get_post_type( $post_id ) ) return;

    ...

Now, the routine will go through an additional check and compare the type of the post being saved with that of the one to which your custom meta box applies. If they don't match, it won't continue. This means pages (and posts) will be unaffected by the function in the future.

(Note: I didn't know the name of your post type, so I guessed above ...)

  • Thanks EAMann for reply. But this solution did not work for me. I have added code which you provide but nothing did work. All goes as same as before. – user2954969 Mar 25 '15 at 6:22
  • 1
    Super..Now it works. Actually I have called function save_post more than one time. So I placed above code in all those functions and worked like a charm. Thanks EAMann.. – user2954969 Mar 25 '15 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.