• Resolved mlaurel

    (@mlaurel)


    Hey all,

    I need an extra set of eyes to help me fix code that automatically sets any post to private so only the admin can publish any post draft.

    The problem I’m running into is that every time a user clicks on the “All Posts”, coming from anywhere in the dashboard, an “auto draft” post is created.

    Here is the code in question (found online & modified for admin only posting):

    //Force posts of custom type to be private
    //…but first make sure they are not 'trash' otherwise it is impossible to trash a post
    
    function force_type_private($post)
    {
        if (($post['post_type'] == 'post')&&(!current_user_can('administrator')))
        {
            if ($post['post_status'] != 'trash')
    		$post['post_status'] = 'private';
        }
    	
        return $post;
    }
    add_filter('wp_insert_post_data', 'force_type_private');

    How can I stop this from adding drafts?

    Per original code “first make sure they are not ‘trash’ otherwise it is impossible to trash a post”.

    I’m not 100% sure but I think it may be causing a problem?

    Any ideas are welcome.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I placed you code on my site and it did not result in any auto draft posts. Since you mentioned it, I did see some older auto draft posts on my site. I think they were a result of visiting a new post screen and abandoning it without entering anything. In any case, I don’t see how your code could be causing auto drafts to be created. It does not even run until a post is already being saved, and it certainly is not adding any additional posts of any sort.

    Thread Starter mlaurel

    (@mlaurel)

    Thanks for the help.

    Not sure what was going on (it was probably as you explained). Ended up double checking code again & ran more tests. It is working properly.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. I blame caching for anything ?? At least it works now.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Need help refining added code in functions.php’ is closed to new replies.