0

Below is my code that will take the post_id and randomly get 3 posts from the same category and store them as custom field. The code is clearly working, because when I click "New Post", I see the custom field being populated, but when I clicked "Publish" or "Save", the code below didn't get executed, from what I understand, Save_Post will be called once when creating new post and one more time when you actually save the post.

Funny thing is that the code below works in Local server, WAMP, but not on my production server, I don't know why. They all use the same plugins.

function update_postmeta($post_id) {
global $post;

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return;
}

unset($rand_id);

$cat_id = get_the_category($post_id);

$args = array(
'showposts' => 3,
  'orderby' => 'rand',
  'cat' => $cat_id[0]->cat_ID,
  );
   $my_query = new WP_Query($args);
  while ($my_query->have_posts()) : $my_query->the_post();
    $rand_id = $rand_id.get_the_ID().',';
  endwhile; update_post_meta($post_id, 'related_id',$rand_id);
  }

add_action('save_post', 'update_postmeta');
0

You have some errors in your code. For example, you unset a not defined variable. Some lines bellow you try to use again a not defined variable....maybe this is what is causing the problems.

Can you try this? (Edited, I think is better get_posts than new WP_Query in this case)

function wpse_update_postmeta($post_id) {

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    $cat_id = get_the_category($post_id);
    $related_posts = array();

    if( !empty($cat_id) ) {

        $args = array(
        'posts_per_page' => 3,
        'orderby' => 'rand',
        'category' => $cat_id[0]->cat_ID,
    );

    $related_posts = get_posts($args);
    }

    if( count($related_posts) > 0 ) {

        $rand_id = array();

        foreach( $related_posts as $related_post ) {
            $rand_id[] = $related_post->ID;
        }

       update_post_meta($post_id, 'related_id', implode(",", $rand_id));
       //Uncomment the next line to check if you get here
       //error_log("Hello! I'm here");

    }

}
add_action('save_post', 'wpse_update_postmeta');
  • I see, I tried your code, it is still not working. I do see the wpse_update_postmeta() fires when I click the "New Post", but it doesn't fire again when I click "Publish". – Noob Coder Apr 20 '14 at 23:37
  • I've tested the code and it works when Publish a new post or Update an old post. That is, every time the Save event occurs. Maybe your problem is related with other code in your plugin/theme or with other plugins you may be using. Use error_log to debug what is happing inside youw save_post hook. – cybmeta Apr 21 '14 at 6:47
  • I've read againg your question and you say "The code is clearly working, because when I click "New Post", I see the custom field being populated". Can you explain what do you mean? The population of fields is not done by the save_post hook; normally it is done via add_meta_box or similar. – cybmeta Apr 21 '14 at 6:53
  • I will try to debug it with error_log. save_post hook is called twice when you create new content, one when you click on "New Post", another when you "Save/Publish". When I click the "New Post", I see the Related_ID field being populated, because of the add_action('save_post','wpse_update_postmeta');, it was suppose to call the wpse_update_postmeta function one more time when I "Save/Publish", but it is not doing it. – Noob Coder Apr 21 '14 at 22:59
  • You are wrong. When you click on "New Post" the save_post hook is not called. I assume that the Related_ID field is a custom field displayed in a custom meta box; the custom field is normally rendered and populated via add_meta_box function and introduced in a custom meta box via add_meta_boxes action. Then, the field is saved via save_post action. – cybmeta Apr 22 '14 at 6:46

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.