0

I want to modify the publishorpend function below to consider the user group of the currently logged in user when deciding whether to set the post status as "pending" or "publish" (in addition to considering whether submission_requires_approval is true or false). For example:

  1. If submission_requires_approval is false, post_status should be set to publish no matter what the user role is (this is how the function works currently).
  2. if current user is a member of administrators or editors, the post status should be set to publish. note, the current user may have more than one role assigned.
  3. If submission_requires_approval is true and if the current user is a group other than admin/editor (or is not logged in at all*) post_status should be set to pending. *this would be ideal but not absolutely necessary
        public function publishorpend( $listing_id ) {
        $listing = \MyListing\Src\Listing::force_get( $listing_id );

        $post_status = mylisting_get_setting( 'submission_requires_approval' ) ? 'pending' : 'publish';

        wp_update_post( [
            'ID' => $listing->get_id(),
            'post_status' => $post_status,
        ] );

    }

I found this function which may be helpful:

function wcmo_get_current_user_roles() {
  if( is_user_logged_in() ) {
    $user = wp_get_current_user();
    $roles = ( array ) $user->roles;
    return $roles; // This returns an array
    // Use this to return a single value
    // return $roles[0];
  } else {
    return array();
  }
}

Thanks!

0

wp_update_post() ends up using wp_insert_post(), which has wp_insert_post_data inside it. The filter "Filters slashed post data just before it is inserted into the database." I think that would be suitable moment to add the custom logic you described as publishorpend method doesn't seem to have other filters available in it.

You could perhaps do something like this,

add_filter('wp_insert_post_data', 'filter_wp_insert_post_data', 10, 2);

function filter_wp_insert_post_data( $data, $postarr ) {
  // Only affect custom post type. Update the post type slug!
  if ( 'my_post_type' !== get_post_type($data['ID']) ) {
    return $data;
  }

  // You may want to have some extra checks so that the status change doesn't affect every post of this type
  // if ( ! some_theme_or_plugin_function_to_check_if_orphaned($data['ID']) ) {
  //   return $data;
  // }

  // assuming the function returns boolean or truthy/falsy value
  $requires_approval = mylisting_get_setting( 'submission_requires_approval' ); 

  // If submission_requires_approval is false, post_status should be set to publish no matter what the user role is (this is how the function works currently).
  if ( ! $requires_approval ) {
    return $data;
  }

  // if current user is a member of administrators or editors, the post status should be set to publish. note, the current user may have more than one role assigned.
  // As a personal preference, check capabilities instead of role. Admins and editors can edit_others_posts
  if ( current_user_can( 'edit_others_posts' ) ) {
    $data['post_status'] = 'publish';
  // If submission_requires_approval is true and if the current user is a group other than admin/editor (or is not logged in at all*) post_status should be set to pending.
  } else if ( $requires_approval ) {
    $data['post_status'] = 'pending';
  }

  return $data;
}

This is just an example and may need some tweaking to match your exact setup and needs.

1
  • Thanks for your help. This didn't quite work for me but it helped me solve the problem in a not so elegant way (your suggestion was elegant--my implementation was not). I'll try to repost my question again now that I know that the situation is more complicated than I initially thought. – Patrick May 14 '20 at 22:34

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.