WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

migrated from stackoverflow.com May 9 '12 at 13:53

This question came from our site for professional and enthusiast programmers.

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 '11 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 '11 at 5:17

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)..."
share|improve this answer
Looks reasonable to me. I guess how you implement the check would depend on what exactly that post meta value is. – BoltClock Jan 8 '11 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 '11 at 5:42

Your Answer


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.