Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to hook into WordPress publish. The code below is what I'm using, but it doesn't appear to be running. I have it in a plugin file that I deactivate/reactivate each time I make changes (not sure if that's necessary to do that though). Post type is "auction".

add_action( 'publish_auction', 'bvf_set_ceiling_once_on_first_publish' );

function bvf_set_ceiling_once_on_first_publish( $post ) {

    $post_id = $post->ID;

    if ( !get_post_meta( $post_id, 'ceiling', $single = true ) ) {

    $reserve = get_post_meta( $post_id, 'reserve', $single = true );

    $ceiling = $reserve * (rand(40,60) / 100);

    update_post_meta( $post_id, 'ceiling', $ceiling );

    }
}

Now I'm trying the following (01/16/13):

<?php
/**
 * @package Bids_Views
 * @version 0.0.1
 */
/*
Plugin Name: Bids/Views
Description: None
Author: Jerry T.    
Version: 0.0.1
*/

add_action('publish_post', 'bvf_set_ceiling');
function bvf_set_ceiling( $post_id ) {

$post = get_post($post_id);

$reserve = get_post_meta( $post_id, 'reserve', true );

$ceiling = $reserve * (rand(40,60) / 100);

update_post_meta( $post_id, 'ceiling', $ceiling );

}

?>
share|improve this question
add comment

3 Answers

function looks fine, $single = true might be causing your problem, replace it with true

share|improve this answer
    
Gave that a try, no change. –  Jerry Tunin Jan 14 '13 at 20:57
    
Just noticed, I don't think publish_{post_type} accepts args, try by declaring global $post;. –  diggy Jan 14 '13 at 21:01
add comment

The function can use as parameter the post id, not the array or object. Check this with a 'var_dump($post); exit;' in the first line of your function.

You can also use the default hook. A small example, but untested, write from scratch on mobile.

add_action('publish_post', 'my-function-on-publish');
function my-function-on-publish( $post_id ) {

    $post = get_post($post_id);

    if ('auction' === $post->post_type ) {
        //My function
    }
}
share|improve this answer
    
Perhaps I'm doing something wrong here. I was under the impression that basically I could throw this into a plugin file and activate that. The code I just tried to see if it worked at all is above. –  Jerry Tunin Jan 16 '13 at 17:02
    
No, your are right here. And yes, you can throw this in a plugin and it should work. –  bueltge Jan 16 '13 at 18:49
    
Still a no go, any suggestions on troubleshooting? –  Jerry Tunin Jan 17 '13 at 15:36
    
do you have output via var_dump() in the conditional, it works on save all post type and the conditional check for your post type and update as example the post meta data. –  bueltge Jan 18 '13 at 19:40
add comment

This is because when you publish the post and 'publish_post' is called, the post meta is not yet saved in the database. update_post_meta() (the function that saves the post meta in the database) is called after 'publish_post'.

See work around here: How to access the post meta of a post that has just been published?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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