• Resolved anurag.deshmukh

    (@anuragdeshmukh)


    I am creating new plugin where I have displayed one form using shortcode. On submission of form, I want to create user and want to add post to my Custom Post Type.

    I am able to create new user and I am using wp_insert_post() to do so. The issue is, post is getting inserted twice (duplicate). Below is my code for this :

    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    	if (isset($_POST['register_doctor'])) {
    
    		$docdata = array(
                        'first_name' => $doc_fname_register,
                        'last_name' => $doc_lname_register,
                        'user_login' => $username,
                        'user_email' => $doc_email_register,
                        'user_pass' => $doc_conf_pass_register,
                        'role' => 'doctor'
                    );
            $user_id = wp_insert_user($docdata);
    
    		$post = array(
    	                'post_title' => $post_title,
    	                'post_content' => 'test',
    	                'post_type' => 'search_doctors',
    	                'post_status' => 'publish',
    	                'post_author' => 1
    	            );
    
            $post_id = wp_insert_post($post, $wp_error = '');
    	}
    }
    

    One proper user is getting created but posts are getting created twice here. I am stucked here.

    Any help on this will be highly appreciated. Thanks in advanced.

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

    (@bcworkz)

    I’m pretty sure your entire code is called twice. Duplicate users are handled differently than duplicate posts, this is why you see one one user added but two posts. There’s not enough context to speculate on why it may be called twice. In certain situations, it’s actually not that unusual for code to be called multiple times. In cases where multiple calls could cause unwanted behavior, it’s always a good idea to do something to ensure the code is not called more than once.

    On completion of your code, you need to set a value or flag that can be checked and only run your code if the value is not yet set. You could check if a post of that title already exists. Or you could instead set a global, transient, cookie, session variable, static, constant, or class property as a flag that prevents your code from running more than once. For example:

    global $ande_flag;
    if ( ! isset( $ande_flag )) {
       // add user and post here
       $ande_flag = true; // this can only run once
    }

    Many feel that the use of globals is poor practice. This is just a simple example, you can set any other kind of value you like.

    Thread Starter anurag.deshmukh

    (@anuragdeshmukh)

    Many thanks !!!

    I forgot to mention that I was calling that function on init and that may be the reason why it was getting called twice. I tried removing that functionality from init and that works for me.

    I will surely try with Global variable.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome!

    For action hook callbacks (to “init” for example), you can prevent repeat calls by having your callback remove itself from the action stack after it does its task the first time through. remove_action('init', 'my_callback_name');

    Considering many coders frown upon the use of globals, this is a more elegant solution.

    Thread Starter anurag.deshmukh

    (@anuragdeshmukh)

    One more question I have if you can help me out in this.

    I have created edit profile page. I have used Advanced Custom Field plugin to create custom fields for a Custom Post Type. I have created one repeater field which is properly displaying in admin panel.

    I want to display that repeater field in page template. I can display the content added via that field but I need to display that field html in page template. Is that possible.

    Thanks in advance.

    Moderator bcworkz

    (@bcworkz)

    When you say “display that field html”, I’m not sure I follow your intent. To me, that sounds the same as displaying the field content. Is it that any HTML entered in the field is stripped out and you don’t want it stripped out? I’m not a serious ACF user, maybe your question would make more sense if I was. Please explain further.

    Thread Starter anurag.deshmukh

    (@anuragdeshmukh)

    Hi,

    sorry for delay in response. I have created 1 repeater field in backend using ACF plugin , where I can input data and on post update it gets updated. I need to display that custom field in my custom template so that I can input/add values to that post from frontend.

    Screenshot : https://prnt.sc/kf52se

    Please let me know if this works. Many thanks.

    Moderator bcworkz

    (@bcworkz)

    Ah, I understand now, thanks for explaining further. I’m sure that is possible. I could explain about form fields and form handling script, which can be used for any sort of user input, but I suspect that would be “reinventing the wheel”. There is likely an easier way using ACF functions. If so, you can call such functions that output the form and handle the form submittal from your custom template.

    I’m not sure what those functions are. I suggest you ask at the dedicated ACF forum where the plugin devs and expert users can help you with the specifics.

    Thread Starter anurag.deshmukh

    (@anuragdeshmukh)

    Sure, I will ask that in ACF forum. Many thanks for the help. ??

    I read what you said but I still keep getting duplicates only sometimes. Other times I don’t get the duplicates. I don’t know why.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘wp_insert_post() creating duplicate posts for Custom Post Type’ is closed to new replies.