Skip Navigation

[Resolved] Add custom field to custom post type

This support ticket is created 5 years, 2 months 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1358113

Tell us what you are trying to do?
I need to add a custom field that populate registered users in a form.
So if Im uploading content using this form, I can choose other registered users to add them as "co-authors"
It has to be able to select multiple options

Once this is done, the post template will show the user (photo+name) with a link to their profile.

What is the link to your site?
Here's the form:
hidden link

And here is an example post (didn't add the field yet)
hidden link

Thanks

#1358299

Hello, Types doesn't currently offer a custom field that allows you to select from existing User profiles. I can think of a few other ways to accomplish something similar. Let me explain those and tell you some pros and cons for each one. Then you can tell me your thoughts and we can go from there.

1. Create a custom checkboxes group or custom multiselect field that includes an option for each User. The label for each option should be the User's name, and the value should be the User's ID. When your site visitors fill out the Form, they can check or select all the different Users they want to include as co-authors.
Pros:
- This approach isn't complex, but it can be tedious to set up and maintain.
- A custom field can be used in custom search Views, so your site visitors could search for posts using this co-author field.
Cons:
- Very tedious to maintain, because you must add and remove options from this custom field manually any time a new User signs up or an old User leaves.
- If your list of Users is long, this type of input can be difficult to use in the Form. Showing 3000 checkboxes or a multi-select field with 3000 options isn't really practical. There is no "auto-suggest" input type so it's difficult to find the option you're looking for.

2. Create a generic checkboxes group field, or multiselect field, in the Form. Use a View of Users to populate the options for this field. Then use the Forms API to capture the User's selections from this input field and save them in a custom field on the post.
Pros:
- Less setup time than option 1, because using a View to populate the field options is much more automated. The list of options is dynamic based on the View of Users, so you don't have to add and remove options manually.
- Like option 1, a custom field can be used in custom search Views, so your site visitors could search for posts using this co-author field.
Cons:
- Requires custom code using the PHP API.
- Like option 1, if your list of Users is long this type of input can be difficult to use in the Form. Showing 3000 checkboxes or a multi-select field with 3000 options isn't really practical. There is no "auto-suggest" input type so it's difficult to find the option you're looking for.

3. Create a proxy post type to represent Users, then set up a many-to-many relationship between the proxy post type and the custom post type created by the Form. This approach is designed to extend Types post relationships to include Users. Here's some additional information about the idea: https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/
Pros:
- Less tedious to maintain because much of the content creation process is automated.
- Accommodates large User lists well, because an autosuggest input field is available for creating relationships between posts in Forms.
- Allows you to create custom searches for Users, and allows for other relationships between Users and Posts. This can be useful for things like Favorites lists.
- Each proxy post gets its own single page, and you can create archives of these proxy posts.
Cons:
- There are some limitations to searching with post relationships that don't apply to searching with custom fields. For example, you can only include one post relationship filter in a custom search.
- It's a bit more difficult to set up because it requires the use of Forms to create proxy posts.

#1358815

Thank you for such a complete answer! so after checking them out, I think we should try first with option n.3.

So, So far I understand:
1. I've to create a new custom post, how do I make it so is a "users post" ?
2. About setting the relationships Im confuse, haven't done that yet, ever, is there any post about how to do it?

Thanks!

#1359137

1. I've to create a new custom post, how do I make it so is a "users post" ?
Okay you will create a new custom post type in Toolset > Post Types. You can call it anything, like "Clients". You will use the Client post type as a proxy for Users. You must create one Client post for each User. Assign each User as the author of their own Client post. Each User can be the author of only one Client post.

That's basically how it works. If you want to automate this process, you can set up a Form to create a Client post from the front-end of the site. Each User must submit the Form to create his or her own Client post, then they cannot create another Client post. See here: https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/#how-do-i-prevent-users-from-creating-more-than-one-contractor-post

2. About setting the relationships Im confuse, haven't done that yet, ever, is there any post about how to do it?
Sure, you will use a many-to-many relationship to link "Clients" and your other custom post type. We have more information about post relationships available here:
https://toolset.com/documentation/post-relationships/
https://toolset.com/documentation/post-relationships/how-to-set-up-post-relationships-using-toolset/

Here's a document showing how to display related posts in a View:
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/#displaying-many-related-items

#1359203

OK! I think Im starting to understand it but slowly......

So I have created the custom post "author":

First problem I find is that when I create a new "author" post I can only see admin users, the rest of them ar "subscribers" and apparently that's why they aren't showing as an option?
Also is a big job to create a post for each (300) user, is it possible to do it automatically, at least once? (new user should do the form thing, next, my questions regarding this)

About this:
"...you can set up a Form to create a Client post from the front-end of the site."
How Can I add a hidden field on the registration form, so everyone that register on the platform, automatically create an "author post" ??

I have also created a relationship between "authors" and "community modules" (the custom post) with no relationship fields, and its state:
"Infinite Authors can be assigned to one Community Module"
"Infinite Community Modules can be assigned to one Author"
This is correct.

Still, don't understand how to add on the custom post form a field with:
- autosuggest input field (with the option to assign/select more than 1 author)
- It has to be a front-end form
- The final "community module post" has to show the list of authors with a link to their profile.

Thanks for your help!!

#1359221

Also is a big job to create a post for each (300) user, is it possible to do it automatically, at least once?
If you have existing Users and you want to create posts for them automatically, you could export those Users somehow and create a CSV file. Then create the Clients custom post type. Then use a CSV import tool to import those entries as Clients.

How Can I add a hidden field on the registration form, so everyone that register on the platform, automatically create an "author post" ??
Okay I'm not sure what the hidden field is for, but I can show you another way. If you want to create the "Client" post automatically after the User Form is submitted, you can use the Forms API cred_save_data and the WordPress API wp_insert_post. Here is an example:

add_action('cred_save_data', 'create_client_post_auto',10,2);
function create_client_post_auto($user_id, $form_data)
{
    if ($form_data['id']==12345)
    {
        // Create post object
$my_post = array(
  'post_title'    => $_POST['first_name'] . ' ' . $_POST['last_name'],
  'post_status'   => 'publish',
  'post_author'   => $user_ID,
  'post_type' => 'client'
);
    
// Insert the post into the database
wp_insert_post( $my_post );
    }
}

You would change 12345 to match the numeric ID of the create User Form, and you can change the post_title if you want. The post author field is automatically set to be the User just created by the User Form.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Still, don't understand how to add on the custom post form a field with:
- autosuggest input field (with the option to assign/select more than 1 author)

The related post autosuggest field only allows you to select one item at a time, and it will be displayed on a Relationship Form, not on the Form that creates a new post.

If you must be able to select more than one item at a time using an autosuggest field, the only other option I know of is a flat taxonomy.

- The final "community module post" has to show the list of authors with a link to their profile.
You can either use the proxy post single page as a profile, or you can use the WordPress Author Archive as a profile. If you want to use the proxy post, create a View of related proxy posts and place that View on the template for the Community Module post. In the View's loop, you can insert a link to each proxy post:

Client profile: [wpv-post-link]

If you want to use the WordPress Author archive instead of the proxy post, you will still create a View of proxy posts. This time, instead of inserting a link to the proxy post in the View loop, you will determine the author of that proxy post and insert a link to the author's archive:

<a href="/author/[wpv-post-author format='meta' meta='user_login']">[wpv-post-author format='meta' meta='user_login']</a><br />

Then you must create an author archive in Toolset > WordPress Archives.
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-author

#1359743
2final result.png
1creating new post.png

OK I'll try my best to do it that way, but it looks too complicated for my point of view of what I'm trying to do:
- To have a custom post with a custom field that lists users (previously selected on a front-end form, which users use to create the custom post)

Just to be 100% I've shared with you a couple of images "simulating" what I'm trying to do in 2 steps, maybe this helps to understand.

Please if you have more simple ideas about how to achieve this, let me know, thanks.

#1359773

Thank you for the extra screenshots. I think we understand each other quite well, and you explained very clearly what you want. Unfortunately I don't have a simple solution to offer for these reasons:
- The easiest way to link Users and Posts is post author. WordPress only allows one post author per post. If you want to associate more than one User with a post, you must use some type of custom solution.
- There is no built-in way to select multiple Users from a single custom field in Types.
- You can't use Toolset's post relationships to link Users and Posts. A proxy post type is required to link Users to Posts via a second post and post authorship.
- There is no built-in way to create links between more than two posts in a post relationship form. You can link two posts at a time, or you can create your own custom solution that allows more than two posts to be related using custom code.

With those reasons in mind, I explained a few alternatives. I am happy to help explain in more detail, or help you build a test site. We have a sandbox environment available at discover-wp.com - feel free to set up a site and we can collaborate on a solution.

#1360403

Thanks for your reply, I understand, just wanted to be sure. 🙂

So Im looking at doing the same but in another way, I'll let you know if I need more help, thanks!

#1360877

I'll stand by for your update.