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

Assign author on ajax wp post insert

programmeradmin1浏览0评论

How i can assign front end post author to current login user. i have front end post submission form (wp_insert_post) threw ajax post request. so user loged in and post via front end form. i need to get which user submited the post. i'm new in coding.

public function submit_testimonial()
    {  
        if (! DOING_AJAX || ! check_ajax_referer('testimonial-nonce', 'nonce') ) {
            return $this->return_json('error');
        }
        $name = sanitize_text_field($_POST['name']);
        $data = array(
            'name' => $name,
        );
        $args = array(
            'post_title' => $name,
            'post_content' => $message,
            'post_author' => 1,
            'post_status' => 'publish',
            'post_type' => 'testimonial',
            'author' => $current_user->ID,
            'meta_input' => array(
                '_zon_testimonial_key' => $data
            )
        );
        $postID = wp_insert_post( $args );
        if ($postID) {
            return $this->return_json('success'); 
        }  
    }
    public function return_json( $status ) {
        $return = array(
            'status' => $status
            );
            wp_send_json($return);    
    }

How i can assign front end post author to current login user. i have front end post submission form (wp_insert_post) threw ajax post request. so user loged in and post via front end form. i need to get which user submited the post. i'm new in coding.

public function submit_testimonial()
    {  
        if (! DOING_AJAX || ! check_ajax_referer('testimonial-nonce', 'nonce') ) {
            return $this->return_json('error');
        }
        $name = sanitize_text_field($_POST['name']);
        $data = array(
            'name' => $name,
        );
        $args = array(
            'post_title' => $name,
            'post_content' => $message,
            'post_author' => 1,
            'post_status' => 'publish',
            'post_type' => 'testimonial',
            'author' => $current_user->ID,
            'meta_input' => array(
                '_zon_testimonial_key' => $data
            )
        );
        $postID = wp_insert_post( $args );
        if ($postID) {
            return $this->return_json('success'); 
        }  
    }
    public function return_json( $status ) {
        $return = array(
            'status' => $status
            );
            wp_send_json($return);    
    }
Share Improve this question edited Jul 9, 2019 at 15:22 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jul 9, 2019 at 7:57 Noufal BinuNoufal Binu 2713 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You’re almost there. Here’s the part that is setting the author of post to user with ID = 1:

    'post_author' => 1,
    'post_status' => 'publish',
    'post_type' => 'testimonial',
    'author' => $current_user->ID,

You even try to set the author to $current_user, but you don’t initiate this variable anywhere in your code and you set author field instead of post_author.

So here’s the correct version:

    'post_author' => get_current_user_id(),
    'post_status' => 'publish',
    'post_type' => 'testimonial',

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论