The problem seems pretty straight forward: Once the save_post action is fired, I would like to append the value of a meta key onto the end of the post_content if it's not already found within the post itself.

I've attempted making a call to wp_update_post() from within a function tied to save_post, but as many of you will immediately object, wp_update_post() contains do_action('save_post') and as such creates an infinite loop.

What other way (using the API) exists for one to update the content of a post based on said posts custom values once the post has been saved/edited without invoking a nightmarish infinite loop?

  • Have you tried hooking into the wp_insert_post_data filter? (Despite the name, it's run during both inserts and updates.)
    – BoltClock
    Jan 8, 2011 at 3:25
  • @BoltClock That was the route I had started down just after posting this question - I've stumbled onto a working example that could no doubt be improved. I'll share it below.
    – Sampson
    Jan 8, 2011 at 5:17

1 Answer 1


The initial solution I've found is to tie a filter onto an action like wp_insert_post_data and extract other post information from the global $post object.

// Tack our filter onto the wp_insert_post_data action
add_filter( 'wp_insert_post_data', 'my_appender' );
function my_appender( $content ) {
  // Bring global $post into scope
  global $post;
  // Get meta value of meta key 'key_name'
  $meta_value = get_post_meta( $post->ID, 'key_name', TRUE );
  // If value is not in content, append it onto the end
  if ( stristr( $content['post_content'], $meta_value ) === FALSE )
    $content['post_content'] .= $meta_value;
  // Return filtered content
  return $content;

I'm certain this could see improvement.


  1. add_filter() - "Filters are the hooks that WordPress launches..."
  2. wp_insert_post_data - "A filter hook called by the wp_insert_post function..."
  3. global keyword - "The scope of a variable is the context within which it is defined..."
  4. get_post_meta() - "This function returns the values of the custom fields..."
  5. stristr() - "Find first occurrence of a string (Case-insensitive)..."
  • 1
    Looks reasonable to me. I guess how you implement the check would depend on what exactly that post meta value is.
    – BoltClock
    Jan 8, 2011 at 5:40
  • @BoltClock Correct. I've intentionally kept this example pretty lightweight. My own production version has all the checks and balances in place.
    – Sampson
    Jan 8, 2011 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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