Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Liam Maclachlan
Liam Maclachlan
22,805 Points

WordPress function is not saving my new, generated post on the first commit. Any ideas?

Developing an audit plugin for work that is not quit going to plan. It needs to generate a new Audit post when the initial save is made form the 'New Post' are of Wordpress. It works once the page has been saved once before.

It seems to be due to the IF statement after the the check as to whether it is a page or not. When that is not there is works... Any idea what I can hook in to to make it save on the initial

<?php
function sliced_create_audit_trail( $post_id ) {

    global $post;

    // 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;

    /* Get the post type object. */
    //$post_type = get_post_type_object( $post->post_type );

    /* Check if the current user has permission to edit the post. */
    //if ( !current_user_can( $post_type->cap->edit_page, $post_id ) ) return $post_id;

    // OK, we're authenticated: we need to find and save the data


    if ( $_POST['post_type'] == 'page' ) {

        // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'sliced_create_audit_trail');

        // If this is just a revision, don't send the email.
        if ( wp_is_post_revision( $post_id ) ) return;

        $post = array(
              'post_title'     => 'Audit Post', // The title of your post.
              'post_type'      => 'sliced_audit_cpt',// Default 'post'. made, in GMT.
        ); 

        wp_insert_post( $post );

        // re-hook this function
        add_action('save_post', 'sliced_create_audit_trail');

    }
    return $post_id;
}
add_action( 'save_post', 'sliced_create_audit_trail' );
?>

My first inkling is that the post is neither a post or page until it has been saved the first time... Any ideas?

1 Answer

Liam Maclachlan
Liam Maclachlan
22,805 Points

Problem solved with another issue I was having. Need to use:

<?php
        $the_post = get_post($post_id);

    if ( $the_post->post_type == 'page'  ) {
                #Code here
        }
?>

instead of

<?php
    if ( $_POST['post_type'] == 'page'  ) {
                #Code here
        }
?>

On the initial save, at the very least .