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 have two plugins, one uses the function wp_insert_post(), the other has a code like this:

add_action('future_to_publish', 'myFunc', 10, 1);
add_action('new_to_publish', 'myFunc', 10, 1);
add_action('draft_to_publish', 'myFunc', 10, 1);

function myFunc( $postID ) {

}

When the first plugin runs wp_insert_post(), $postID is always empty. If I use the hook "publish_post" and press update then $postID does have a value, so what I am doing wrong?

share|improve this question
add comment

3 Answers

  1. Read the Codex,
  2. Look at the function in the core file,
  3. Modify your code as follows:

    function myFunc( $post ) {
    
        $postID = $post->ID;
    
    }
    

The post transistion does not send the post ID, it sends the complete post object. Sometimes a simple die(var_dump($postID)); (or whatever you use as parameter) helps to find out what will be send to the callback. If you don't know how many parameters are send to the callback, put a die(var_dump(func_get_args())); at the first line of your callback.

share|improve this answer
add comment

Use wp_insert_posts():

$postId = wp_insert_post($args);
if(!$postId){
echo "Your post is not inserted that's why you've no post id :p"
}
share|improve this answer
add comment
function myFunc( $post ) {
    global $post;
    $myPostId = $post->ID;

    //your function here

}

For getting the $postID value, you just insert global $post expression in your function. It worked for me.

share|improve this answer
1  
This does not appear to answer the question. –  s_ha_dum Jun 5 '13 at 21:59
1  
"This answer was automatically flagged as low-quality because of its length and content", which I am sure you don't want. Please explain what your code does and why it would solve the problem. –  s_ha_dum Jun 5 '13 at 22:01
    
It is answering the question. JUST READ THE ENTIRE ORIGINAL QUESTION: "$postID is always empty". For getting the $postID value, you just insert global $post expression in your function. @s_ha_dum, I appreciate your answer but you become too zealous (not jealous) in your willing to help –  Val Jun 8 '13 at 7:03
    
See the answer by @Ralf912 –  s_ha_dum Jun 8 '13 at 12:16
    
answer by @Ralf912 it is not complete! you MUST call the global global $post; if it's a plugin file - which it is. Again, READ the freaking question, this is not poker to read the players. –  Val Jun 8 '13 at 15:19
show 1 more 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.