Skip Navigation

[Resolved] Put number (example: 5) on custom field and make 5 custom post types with form.

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 15 replies, has 2 voices.

Last updated by darioM-3 4 years, 1 month ago.

Assisted by: Jamal.

Author
Posts
#2110525
Screenshot 2021-07-09 at 23.15.18.png

Tell us what you are trying to do?
Hello support,
Is it possible to create with a custom field on the post form for creating custom post types, where put number (example: 5) for 5 creations of custom post types and automatic create 5 posts from one submission?

Is there any documentation that you are following?
Not found

Is there a similar example that we can see?
Not found

What is the link to your site?
It is on localhost

#2110877

Hello and thank you for contacting Toolset support.

I am really sorry, but I don't understand very well what you want to achieve. Can you provide a detailed example in a way such as:
1. I go to xxx
2. I do xxx
3. I expect to have xxx
4. Instead, I get xxx

In the meantime, feel free to check our Toolset Forms hooks, you will most probably need to use the cred_save_data and wp_insert_post.
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
- https://developer.wordpress.org/reference/functions/wp_insert_post/

Please provide more details so I can give accurate advice.

#2110901

Hello Jamal,

Sorry for the confusing explanation. I try to be much more clear.

1. I go to create my custom post type, after that create my post custom form, after that create my custom field on the post custom form, then put in my custom field number (for example: 5)
2. I do click on the form button Create
3. I expect to have 5 custom posts in my wp-dashboard

Probably I need some snippet of the code to put in the custom field for that or?

I hope that I am clear this time, if not I send screens of images one by one that I need.

Thank you.

#2110933

Thank you for your feedback but it is not yet clear for me to understand exactly what you want to achieve. Let me write below what I understood so far and correct me if I missed something.

Let's say you have a custom post type Events. With some custom fields.
You have created a form for this post type.
You want to have a field in the form, when you input a number(for example 5), and after submitting, you would like to have 5 events posts created.
Did I understand correctly? Is this about one custom post type or different custom post types?

Assuming that I understand correctly. Here what you will do:
- In the form, instead of using a defined custom field for the number of posts to create, use a generic field. Generic fields are not persisted in the database and are meant to be used in custom code.
- Add custom code that will create the number-1. Because the form will already create one post.
- The custom code will be hooked to the cred_save_data, which is executed after the form's post is saved.

Check this scoped search to see some example of using wp_insert_post for creating posts hidden link

If I did not understand correctly, or if you an example of the code, please share as many screenshots as possible. From the post type definition, from the form.

#2110965

Hello again Jamal,

"Let's say you have a custom post type Events. With some custom fields.
You have created a form for this post type.
You want to have a field in the form, when you input a number(for example 5), and after submitting, you would like to have 5 events posts created.
Did I understand correctly? Is this about one custom post type or different custom post types?"

This part you understand correct. In general, I need only one custom field where I input a number of posts (example: 5) and make 5 posts of events and that's it. Only I need a custom code snippet that helps me to make multiple custom posts of Events.

I check the link that you send me: hidden link
but I cannot see there some close example of snippet code what I exactly need.

I hope that now I am clear? Let me know if you need something better explanation.

Thank you one more time.

#2110969

Thank you very much for this, it is clearly a bit, but not that clear to provide a code snippet that works. I am afraid, I'll provide an example, but then, we'll find out that it misses something else that you did not explain yet.

Can you provide a functional explanation. Why would you like to create a number of posts at once? Maybe you are representing cars, horses, or what your website is about. And maybe you also want some other custom fields to be saved.

The code that I may provide needs some details, like the slug of the post type, any custom fields that you want to have, maybe some other bits of information. Etc.

So, I'd like to ask what would you like to achieve?
Please explain the features you want to make like if you want to explain to your project manager or your developer? In terms of features and functionalities, you want to have.
I am afraid, your question is rather general, and I want to get to a solution the fastest way possible 🙂

#2111003
6.png
5.png
4.png
3.png
2.png
1.png

Hello again Jamal,

I try to explain close as possible and provide screens and numbers with the process.

1. I want to make a custom post Type called Sticker ID.
2. There I make a 3 categories called Main white, sub white ...
3. After that I make a form in which I display category field like dropdowns.
4. I make when to click on create button automatic create 1 post of sticker id and put it on the chosen category from the dropdown field.
5. I make when clicking on the button "Create" already that automatically generate a title (custom combination characters) and as I said before creating one post of the sticker.
6. I need now to make another custom field where I input the number of the posts (example: 5 or 100) and then click on the "Create" button to create that number of the posts. And for that custom field of making multiple posts, I need a snippet of the code.

That's the whole process.

I finally hope that now clear how much as possible? 😀

Thank you.

#2111053

I am really sorry, but that's not clear enough to provide an example of custom code. Please answer these questions:
- Why would you need to have a custom field for the number of posts? Does it need to be saved for each post?
- Is this about the same form as the ticket here https://toolset.com/forums/topic/want-generate-8-characters-on-post-titles-of-custom-post/
- Can I log in to your website and check it closely? This will give me a better idea. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#2111063

Hello Jamal again,

- Can I log in to your website and check it closely? This will give me a better idea. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
The page is not online yet, so I cannot provide data access information. It is on localhost.

- Why would you need to have a custom field for the number of posts? Does it need to be saved for each post?
Not data to be saved for each post. Everything working fine on my form now. Only what I need is a custom field on my form to put the number of how much I want to create a stickers (read posts) there, because sometimes I want to create multiple (5 at one click, sometimes 10 at one click, depending on which number I put on the custom field) stickers (read posts) when one click on Create button of the form. Not only to Create one post like it is now.
I don't know how different to explain the situation. 😀

- Is this about the same form as the ticket here https://toolset.com/forums/topic/want-generate-8-characters-on-post-titles-of-custom-post/

Yes.

#2111323

Awesome, so, I'll update that code with the part that will create the additional posts.

As I have explained earlier, if you don't need to store the field "number of posts/stickers" in the database, you should use a generic field. Check this screenshot hidden link
Add a unique slug to the generic field, because we'll use it in the custom code. Check this screenshot hidden link

And then we can update the snippet to:

add_action('cred_save_data', 'func_build_auto_post_title', 10, 2);
function func_build_auto_post_title($post_id, $form_data) {
	if ($form_data['id']==2103) {
		$myRandomString = generateRandomString(8);
		$slug = sanitize_title($myRandomString);
		wp_update_post(array('ID'=>$post_id, 'post_title'=>$myRandomString ,'post_name' => $slug));
		
		$number_of_posts = (int) $_POST['number']; // this is the field's slug;
		if ( $number_of_posts > 0 ) {
			for ($i = 1; $i < $number_of_posts; $i++) {
				wp_insert_post(array(
					'post_title' => generateRandomString(8),
					'post_type' => 'sticker', // this the post type slug
					'post_status' => 'publish'
				));
			}
		}
	}
}
 
function generateRandomString($length = 25) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

Please update line 3 with your form's ID, line 13 with the custom post type slug, and line 14 with the status that you want to have for the created posts.

#2111389
Screenshot 2021-07-11 at 14.08.18.png
Screenshot 2021-07-11 at 13.59.39.png

Hello Jamal again,

Thank you a lot. Code working exactly what I want.
Only what is wrong now it's that only one sticker has a category which I put (Main blue, sub white) that belongs (the rest empty, see attach).

Example: I now put number 5 to generate 5 stickers (posts) numbers but only 1 sticker has a category, 4 not?

Thank you.

#2111431

Sure, check this new code, and modify lines 8 and 20 with your taxonomy slug. My taxonomy slug is "subject-type"

add_action('cred_save_data', 'func_build_auto_post_title', 10, 2);
function func_build_auto_post_title($post_id, $form_data) {
	if ($form_data['id']==2103) {
		$myRandomString = generateRandomString(8);
		$slug = sanitize_title($myRandomString);
		wp_update_post(array('ID'=>$post_id, 'post_title'=>$myRandomString ,'post_name' => $slug));
		
		$terms = wp_get_object_terms( $post_id, 'subject-type');
		$terms = wp_list_pluck( $terms, 'term_id' );
		
		$number_of_posts = (int) $_POST['number']; // this is the field's slug;
		if ( $number_of_posts > 0 ) {
			for ($i = 1; $i < $number_of_posts; $i++) {
				$id = wp_insert_post(array(
					'post_title' => generateRandomString(8),
					'post_type' => 'sticker', // this the post type slug
					'post_status' => 'publish'
				));
				if( count($terms) > 0 ){
					wp_set_object_terms($id, $terms, 'subject-type');
				}
			}
		}
	}
}
 
function generateRandomString($length = 25) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

I hope this helps. Let me know if you have any questions.

#2111445
Screenshot 2021-07-11 at 16.52.51.png

Hello Jamal again,

This code not work.
In this case, I put my taxonomy slug "main-black-sub-white" and use that category in the dropdown and again I put number 5 in custom number field to generate 5 stickers (posts) numbers but only 1 sticker has a category, 4 not.

See My code:

add_action('cred_save_data', 'func_build_auto_post_title', 10, 2);
function func_build_auto_post_title($post_id, $form_data) {
    if ($form_data['id']==10) {
        $myRandomString = generateRandomString(8);
        $slug = sanitize_title($myRandomString);
        wp_update_post(array('ID'=>$post_id, 'post_title'=>$myRandomString ,'post_name' => $slug));
         
        $terms = wp_get_object_terms( $post_id, 'main-black-sub-white');
        $terms = wp_list_pluck( $terms, 'term_id' );
         
        $number_of_posts = (int) $_POST['number']; // this is the field's slug;
        if ( $number_of_posts > 0 ) {
            for ($i = 1; $i < $number_of_posts; $i++) {
                $id = wp_insert_post(array(
                    'post_title' => generateRandomString(8),
                    'post_type' => 'stickers-id', // this the post type slug
                    'post_status' => 'publish'
                ));
                if( count($terms) > 0 ){
                    wp_set_object_terms($id, $terms, 'main-black-sub-white');
                }
            }
        }
    }
}
  
function generateRandomString($length = 25) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

Also, there I have three categories that I want to include with the dropdown categories field, not only one.

Thank you.

#2111475

I'll need to take a closer look at your site to check this further. I will also need FTP access to work on the code and to check the debug.log file. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#2111967

Because you are using the default category taxonomy, we'll need to use wp_get_post_categories and wp_set_post_categories to read and assign the category terms to the newly created posts. The final code is:

add_action('cred_save_data', 'func_build_auto_post_title', 10, 2);
function func_build_auto_post_title($post_id, $form_data) {
    if ($form_data['id']==8) {
        $myRandomString = generateRandomString(8);
        $slug = sanitize_title($myRandomString);
        wp_update_post(array('ID'=>$post_id, 'post_title'=>$myRandomString ,'post_name' => $slug));
         
        $terms = wp_get_post_categories( $post_id );
         
        $number_of_posts = (int) $_POST['number']; // this is the field's slug;
        if ( $number_of_posts > 0 ) {
            for ($i = 1; $i < $number_of_posts; $i++) {
                $id = wp_insert_post(array(
                    'post_title' => generateRandomString(8),
                    'post_type' => 'sticker-id', // this the post type slug
                    'post_status' => 'publish'
                ));
              	if( count($terms) > 0 ){
                  wp_set_post_categories( $id, $terms);
                }
            }
        }
    }
}
  
function generateRandomString($length = 25) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

Please test it from your side and confirm that it works as expected.