Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Liam Maclachlan
Liam Maclachlan
22,805 Points

Quick edit is deleting my linked CPT. Any ideas on stopping this?

I'm creating a custompost (audit) that keeps a track of all interactions with a post. This works absolutely fine when all interactions go through the edit.php screen.

When the quick edit is used, the audit is deleted. Not sent to the trash; it is competely removed from the face of the earth.

Any ideas?

Code below:

function sliced_main_aduit_func( $post_id) {

    ## 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;
    if ( get_post_status( $post_id ) == 'auto-draft' ) return;
    if ( wp_is_post_revision( $post_id ) ) return;

    ## Get the post object 
    $the_post = get_post( $post_id );

    ## If the saved post type is Page, run main audit log function
    if ( $the_post->post_type == 'page' && current_user_can( 'edit_pages' )  ) :

        ## Unhooked to avoid an infinite save loop when Audit is saved
        remove_action('save_post', 'sliced_on_save_audit_trail');

        ## initialise the main array to hold the Audit posts information
        $initialPostInfo = array();

        ## If page already has an audit, apply the Audi_ID to update the correct audit trail 
        if ( $audit_id = get_post_meta( $post_id, 'audit_id', true ) ) :

            $initialPostInfo['ID'] = $audit_id;
            $initialPostInfo['post_content'] = $the_post->post_content . ' > ' . $audit_id;

        else : 

            ## CPT added if audit for page does not already exists, otherwise the Audit_ID is used to update correct audit
            $initialPostInfo['post_type']   = 'sliced_audit_cpt'; // post type to save to
            $initialPostInfo['post_content'] = 'First commit';


        ## General Audit post informaiton
        $initialPostInfo['post_title']      = $the_post->post_title; // The title of your post.
        $initialPostInfo['post_status']     = 'publish';

        ## Create the new post and apply the ID to a variable, at the same time
        $newPost = wp_insert_post( $initialPostInfo, true );

        ## If no error was returned from the new post creation, and the newly created audits posts ID as a custom field to the original pages meta  
        if ( !$wp_error ) : 

            update_post_meta( $post_id, 'audit_id', $newPost );


        ## re-hook the save post to allow the audit trail to continue on next submission
        add_action('save_post', 'sliced_on_save_audit_trail');      


    return $post_id;

add_action( 'save_post', 'sliced_main_aduit_func' );

2 Answers

Liam Maclachlan
Liam Maclachlan
22,805 Points

Sorted! Moved the line:


    $initialPostInfo['post_type']       = 'sliced_audit_cpt'; // post type to save to

outside of the conditinal it was in and it resolved it. If anyone can explain this, I will happily give them the 'Best Answer' status for this post :)

Hi Liam,

This may not answer your question, but if I'm understanding you correctly, attempting to edit CPT from the quick edit screen sends the edit to the ether. You might take a look here: Understanding the WordPress Quick Edit Custom Box - it appears to touch on what you're asking (and despite the 2012 date, still relevant.) It sounds like making edits through the QE screen is more complicated than it appears.

Best, Cena

Liam Maclachlan
Liam Maclachlan
22,805 Points

Hi Cena.

Really, albeit confusing, great article :) I have bookmarked it for when I need to edit/add to the quick-edits.