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 am using the hook publish_post to run my code where I need the post meta of the just published post. But the post meta value I am looking for is somehow not available at this point of execution. On checking the wp_postmeta table, I found that my meta key value hasn't been created yet. Also, I want this to work for a post published for the first time. Is there any other hook that can give me access to it?

function push_notification($post_id) 
{ 
    $ref_ids = get_post_meta($post_id,'ref_id'); 

    if($ref_ids) 
    { 
        //my code goes here 
    } 
} 

add_action('publish_post','push_notification');
share|improve this question
    
Why don't you hook onto save_post and check for post_status equal to publish, i.e if ( 'publish' == $post->post_status ) ..etc and if you only want to run this function on newly published posts, not re-published/updated posts you could look at wp_transition_post_status and check for $old_status against $new_status –  userabuser Oct 19 '12 at 11:52
    
@toscho The get_post_meta($post_id,'ref_id') returns nothing. Therefore, my code is never executed. –  Poulomi Nag Oct 19 '12 at 11:53
    
@PoulomiNag Try one of the methods listed here and report back please. It would also be useful to see how you add the post meta value. –  toscho Oct 19 '12 at 12:00
    
@userabuser Same problem with save_post. No post meta available. I should have mentioned before that I want this to work for a post published for the first time. And save_post gives access to post meta only at updates. –  Poulomi Nag Oct 19 '12 at 12:27
2  
I tested your code and it works just fine. The problem is elsewhere. That is, the problem is with how and maybe when you are setting the 'ref_id' postmeta value. At least one other person has wondered about that code already. Please edit your question to include the code that creates the 'ref_id' value. –  s_ha_dum Oct 19 '12 at 13:48

3 Answers 3

You seem to be concerned only with new posts (as previously-created posts will have the postmeta attached and available when transitions are run), and want to do push_notification on newly-created posts only if a certain postmeta value is present. Correct?

Create your own hook in the function that saves your postmeta, after all of your update_postmeta calls, using do_action(). The postmeta will have been saved at that point, and you can test.

share|improve this answer
    
Thanks man! You hinted the answer ;) –  Poulomi Nag Oct 20 '12 at 7:36
up vote 0 down vote accepted

Solved it! Used save_post to run push_notification() as well as to run the function save_post_meta() that saves my post meta. The problem occurred coz push_notification() gets fired before save_post_meta() due to which the meta wasn't being saved and therefore it remained inaccessible. Just changed priorities of the functions to make it work like so :

function push_notification($post_id) 
{ 
  $ref_ids = get_post_meta($post_id,'ref_id'); 

  if($ref_ids) 
  { 
    //my code goes here 
   } 
} 
add_action('save_post','push_notification',11,1);

function save_post_meta($post_id,$post)
{
   //check for nonces and current user capabilities

   $ref_id = sanitize_html_class($_POST['ref_id']);
   update_post_meta($post_id,'ref_id',$ref_id);
}
add_action('save_post','save_post_meta',10,2);

function no_notification()
{
  remove_action('save_post','push_notification',11,1);
}
add_action('publish_to_publish','no_notification');

The last function no_notification() makes sure that push_notification() gets fired only when a post is first created and not for updates.

share|improve this answer

I also noticed that when you call get_post_meta() within add_action('publish_post',..) no post meta is returned.

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'. That's why it works if you save as draft first and then publishes.

To avoid that issue I use this kind of code:

$post_meta_value = get_post_meta($post_id,'meta_key',true);
if($post_meta_value==''){ $post_meta_value = $_POST['meta_key']; }

This will get your value from the $_POST when you click on Publish button.
The $_POST['meta_key'] can be different depending on how you set your post meta or custom field in your back-end. I'm using the ACF (Advanced Custom Fields) plugin and it would be $_POST['fields']['field_52fae7b2b4033'].

Hope that helps.

share|improve this answer

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.