1

For this particular case, all posts had to be a child term assigned to be published.

I'm trying to hook on the "save_post" action, check the terms, return an error message if no child term is assigned, and obviously stop saving the post.

This is my approximation, that is doing nothing now, any ideas in what is wrong?

function validate_terms_se( $post_id ) {

  global $post;

  if(isset($post) && $post->post_type != 'activity')
  return $post_id;

  // get all assigned terms   
  $terms = wp_get_post_terms( $post_id, 'activity_term' );
  $only_parent = 'yes';

  foreach( $terms as $term ) {
    if ( $term->parent != 0 ) {
      $only_parent = 'no';
    }
  }

  if( $only_parent == 'yes' ) {
    add_settings_error(
      'invalid-term',
      '',
      'You must select a child term.',
      'error'
    );
  }

}
add_action('save_post', 'validate_terms_se');
  • A dead dog knows more about jquery than me, but anyways, I feel that would be a better solution that a PHP one – Pieter Goosen Jul 6 '16 at 12:35
  • Then, I need this dead dog helping me ;) – Capiedge Jul 6 '16 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.