0

I am currently writing a custom theme from scratch, which utilizes a number of custom post types. I am currently trying to build custom posting forms for them. I have hidden all of the normal form fields, and want the title/slug to populate based on one of the custom fields. So far, the metainformation is saving fin, but the title/slug is always 'autodraft'. Does anybody know how I might fix this?

I have posted the relevant code snippets below.

from functions.php

include_once( 'init/custom_post_types.php' );

from init/custom_post_types.php

function save_custom_post_form( $post ) {
    if( file_exists( dirname( __FILE__  ) . '/../submission_handling/' . $_POST ['post_type'] . '.php' ) )
        include_once( dirname( __FILE__  ) . '/../submission_handling/' . $_POST ['post_type'] . '.php' );
}
add_action( 'save_post', 'save_custom_post_form');

submission_handling/fixture.php (called when $_POST['post_type'] == 'fixture')

<?php
if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])){exit();}

// save metainformation from custom form. This code works fine

$fixturedate = strtotime( $_POST['fixture-date'] );
update_post_meta($post, 'fixture-date', $fixturedate);
$fixturekickoff = esc_attr( $_POST['fixture-kickoff-time'] );    
update_post_meta($post, 'fixture-kickoff-time', $fixturekickoff);
$fixtureoppteam = esc_attr( $_POST['fixture-opposing-team'] );
update_post_meta($post, 'fixture-opposing-team', $fixtureoppteam);
$fixturepat = esc_attr( $_POST['fixture-player-arrival-time'] );
update_post_meta($post, 'fixture-player-arrival-time', $fixturepat);
$fixtureoppteamurl = esc_attr( $_POST['fixture-opposing-team-website-url'] );       
update_post_meta($post, 'fixture-opposing-team-website-url', $fixtureoppteamurl);
$fixtureaddy = esc_attr( $_POST['fixture-address'] );
update_post_meta($post, 'fixture-address', $fixtureaddy);
$fixface = esc_attr( $_POST['fixture-facebook-event'] );
update_post_meta($post, 'fixture-facebook-event', $fixface);

// Save title and slug into new post. This doesn't seem to work

if ( ! wp_is_post_revision( $post ) ){
    $posttitle = array (
         'ID' => $post,
         'post_title' => $fixtureoppteam,
         'post_name' => $fixtureoppteam,
     );

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

     // update the post, which calls save_post again
     wp_update_post( $posttitle );


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

EDIT

Ok, with a bit of debugging, I have worked out that wp_is_post_revision( $post ) is returning the post ID of $post, which is why the post updating code is not firing.

After looking at the raw database I have worked out the following.

When I add a new post, TWO entries are made in the database, with statuses of 'inherit' and 'publish' respectively. It seems that $post is taking the id of the 'inherit' post, which is why wp_is_post_revision ( $post ) is failing. The odd thing is that all those update_post_meta ( ... ) entries are also using $post but the meta entries end up linked to the correct post (the 'publish' post).

I still have no idea how to solve this problem without removing the wp_is_post_revision ( $post ) check, which I don't think is a great idea.

0

Ok, so after reading the codex page on the 'save_post' hook, I spotted that they use a different way of dealing with revisions. So removing the if {...} surrounding the wp_update_post( $posttitle ) and inserting the following code fixed my problem:

if ( $post = wp_is_post_revision( $post ) )
        $post = $post; 

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.