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

I'm trying to restrict how many items you can associate with a post so in my save function I have:

add_action( 'save_post', array( $this, 'save_custom_items_data' ), 10, 2 );

public function save_custom_items_data( $post_id, $post ) {
    if(count($related_items) > 5) {
        // display error message here but the page redirects anyway...
    } else {
        update_post_meta( $post_id, '_custom-meta-items', $related_items );

how do I display an error here instead of saving the custom post meta?

share|improve this question
There's not enough code here to be useful. – vancoder Apr 8 '13 at 21:14
added some more code – keeg Apr 9 '13 at 19:49
up vote 2 down vote accepted

Short of doing some jQuery validation, I think the only option is wp_die().

add_action( 'save_post', array( $this, 'save_custom_items_data' ), 10, 2 );

public function save_custom_items_data( $post_id, $post ) 
    if( count( $related_items ) > 5 ) 
            'Error, 5 items max.', 
                'response' => 500, 
                'back_link' => true 
        update_post_meta( $post_id, '_custom-meta-items', $related_items );

Related Q&A's:

share|improve this answer

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.