最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Allow Post Author to be 0 on Update

programmeradmin3浏览0评论

I have a custom post type that allows registered users to submit videos to my site. I have modified the script so that non-logged in (guest) users can also submit videos. I've added first-name, last-name, and email meta fields for the post so I can capture who posted it and display them in the Author column in the admin area. Any video that was posted by an actual registered user will display in the Author column accordingly as well. I have all of the front end views configured to show either the meta values for the guest user or the registered author information.

The issue I am having is when it comes to editing the post which admins have to do in order to approve it, mark it as featured, etc. These posts have an author ID of 0 and when an admin takes action on the post, the author becomes updated to the logged in admin's user id.

Let's back up a little. At first when I was going to edit one of these guest video posts, I noticed the author box had an admin name selected because the actual author (guest with no user account) was not available on the list. Using jquery, I have added a "Guest" option to the top of the author meta box and I have set the value to 0 (I even tried just empty value). The idea being, if the post is by a guest, I would like to just leave that value alone and make edits to the post with the author still being 0 OR I can manually override the author and set it to a particular user.

The issue is that even with the jQuery in place and leaving the selection on Guest, the post still gets updated and attributed to the current logged in admin.

function add_null_author_option() {
  global $post;
  $post_id = $post->ID;
  if(!$post->post_author) {
    $first_name = get_post_meta($post_id, 'first-name', true);
    $last_name = get_post_meta($post_id, 'last-name', true);
    echo "<script>
      jQuery(document).ready( function() {   
          var post_id = $post_id,
          first_name = '$first_name',
          last_name = '$last_name';
          console.log('Post: ' + post_id);
          console.log('First Name: ' + first_name);
          jQuery( '#post_author_override' ).prepend( '<option value=\"\" selected>Guest - ' + first_name + ' ' + last_name + '</option>' );
      });
      </script>";
  }
}
add_action('admin_footer-post.php', 'add_null_author_option');
add_action('admin_footer-post-new.php', 'add_null_author_option');

I'm not sure if it's an issue with wp_update_post() function defaulting to the current user ID where post_author value is 0 or null. If so, is there any way to bypass that just for my custom post type?

I have a custom post type that allows registered users to submit videos to my site. I have modified the script so that non-logged in (guest) users can also submit videos. I've added first-name, last-name, and email meta fields for the post so I can capture who posted it and display them in the Author column in the admin area. Any video that was posted by an actual registered user will display in the Author column accordingly as well. I have all of the front end views configured to show either the meta values for the guest user or the registered author information.

The issue I am having is when it comes to editing the post which admins have to do in order to approve it, mark it as featured, etc. These posts have an author ID of 0 and when an admin takes action on the post, the author becomes updated to the logged in admin's user id.

Let's back up a little. At first when I was going to edit one of these guest video posts, I noticed the author box had an admin name selected because the actual author (guest with no user account) was not available on the list. Using jquery, I have added a "Guest" option to the top of the author meta box and I have set the value to 0 (I even tried just empty value). The idea being, if the post is by a guest, I would like to just leave that value alone and make edits to the post with the author still being 0 OR I can manually override the author and set it to a particular user.

The issue is that even with the jQuery in place and leaving the selection on Guest, the post still gets updated and attributed to the current logged in admin.

function add_null_author_option() {
  global $post;
  $post_id = $post->ID;
  if(!$post->post_author) {
    $first_name = get_post_meta($post_id, 'first-name', true);
    $last_name = get_post_meta($post_id, 'last-name', true);
    echo "<script>
      jQuery(document).ready( function() {   
          var post_id = $post_id,
          first_name = '$first_name',
          last_name = '$last_name';
          console.log('Post: ' + post_id);
          console.log('First Name: ' + first_name);
          jQuery( '#post_author_override' ).prepend( '<option value=\"\" selected>Guest - ' + first_name + ' ' + last_name + '</option>' );
      });
      </script>";
  }
}
add_action('admin_footer-post.php', 'add_null_author_option');
add_action('admin_footer-post-new.php', 'add_null_author_option');

I'm not sure if it's an issue with wp_update_post() function defaulting to the current user ID where post_author value is 0 or null. If so, is there any way to bypass that just for my custom post type?

Share Improve this question asked Jan 21, 2021 at 22:26 bowleraebowlerae 1112 bronze badges 2
  • I believe the way the WordPress table is structured you may not be allowed to have a value lower than 1. I'm going to go check... ...but I think you'd be better off making a 'generic' or anonymous author and assigning that value instead of 0. BRB. – Tony Djukic Commented Jan 22, 2021 at 1:15
  • Update: It allows me to make a new post entry directly in the DB that has an author of 0 so that's not the problem. – Tony Djukic Commented Jan 22, 2021 at 1:40
Add a comment  | 

1 Answer 1

Reset to default 0
function preserve_anonymous_author($data, $postarr) {
    // Check if this is an update (not a new post)
    if (!empty($postarr['ID'])) {
        $existing_author = get_post_field('post_author', $postarr['ID']);
        
        // If the existing author is 0, keep it 0
        if ($existing_author == 0) {
            $data['post_author'] = 0;
        }
    }
    return $data;
}
add_filter('wp_insert_post_data', 'preserve_anonymous_author', 10, 2);
发布评论

评论列表(0)

  1. 暂无评论