0

So I've created a function that updates several custom fields in a post when the post is created. The idea is to sanitize this information so that I can easily use it for other purposes. The function works fine when I create a post manually. However, I import hundreds of posts directly in to the site (using WP All Import), and for these posts that are imported, it seems that this function does not apply. Currently I'm using the 'save_post','publish_post', and 'new_to_publish' hooks to try to accomplish this. However none of them seem to work. Am I missing something? Is there a better way to accomplish this? Here's abridged version what I have thus far:

add_action( 'save_post', 'sterilize_vehicle_information' );
add_action( 'publish_post', 'sterilize_vehicle_information' );
add_action( 'new_to_publish', 'sterilize_vehicle_information' );
function sterilize_vehicle_information( $post_id ) {
    $internet_price = get_field('internet_price',$post_id);
    update_post_meta( $post_id, 'internet_price', preg_replace( "/[^0-9]/", "", $internet_price ) );
}

Thanks for the insight!

  • I found a fellow on SO who has an almost identical problem to my own. He posted up a solution, but when I use the "updated_post_meta" or "added_post_meta" the website redirects to a 404. Anyone have more experience with this? stackoverflow.com/questions/12809525/… – Steve Dimock Jun 17 '13 at 22:22
1

There is a hook that worked for me in most case while those 3 hooks you use fail. It's transition_post_status :

add_action('transition_post_status', 'sterilize_vehicle_information', 10, 3);
function sterilize_vehicle_information( $post, $new_status, $old_status) {

   $internet_price = get_field('internet_price',$post->ID);
   if(  $new_status == 'publish' && old_status != 'publish' ) {
           update_post_meta( $post->ID, 'internet_price', preg_replace( "/[^0-9]/", "", $internet_price ) );
    }
}

Hope this will help.

  • Thanks for the response, Tweet. Interestingly I gave this a shot, and it seems to conflict with another plugin, Advanced Custom Fields, giving me this error: "Catchable fatal error: Object of class WP_Post could not be converted to string in" . Unfortunately I did try it a few different ways, and it still seems to not be working. – Steve Dimock Jun 15 '13 at 5:54
  • I've edited the function. Is it working now? – JMau Jun 15 '13 at 10:06
  • Hey Tweet. Tried it, and I'm still getting that error. I am currently testing the pmxi_saved_post hook used by WP Import, but it still doesn't seem to be working. – Steve Dimock Jun 17 '13 at 20:42
  • @DEUTSCHWULF I know it has been a whole since you posted this, but do you happen to remember how you accomplished it? – SAHM Oct 19 '15 at 4:16
  • Hey SAHM, if I recall correctly this never worked the way that I wanted it to. I ended up just writing a script of my own that would import everything in the way that I needed to. I'm sorry I couldn't be of more help. – Steve Dimock Oct 20 '15 at 4:04

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.