Navigation überspringen

[Gelöst] Create dropdown with users

This support ticket is created vor 5 Jahren, 1 Monat. 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)

Dieses Thema enthält 29 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von Christian Cox vor 5 Jahren, 1 Monat.

Assistiert von: Christian Cox.

Author
Artikel
#1378269

On the front-end of the site, you can use that User ID to display other User information in a View or Content Template. That is definitely possible. However, on the back end post editor this field just holds a number, so any front-end custom search filter based on this field will only display the numeric values.

This is the real crux of the issue and the real reason why the custom field approach is not practical in your case. It's difficult to manage in wp-admin because you must know the User IDs you want to connect to any post, if you wanted to connect them manually in wp-admin. It's difficult to use in custom search because you can't map the User ID to other User data (like User display name, first name, last name, etc) easily in a custom search filter's options. So the filter options are nonsensical to your site visitors. The proxy post type solves this problem because the post title and post ID are automatically mapped to one another in custom search filters.

#1378305

I'm good with the concept. I'm just having trouble with the implementation. I created the 'contractors' & 'showcases' CPTs just to follow the example and get things going. As I understand it, what I need to do is generate forms to register new user 'to' the 'contractors' CPT which would then create the user profile? How is this helping?

#1378319

Let's assume Contractors is the proxy post type for Users. If you plan to create the Contractor post automatically when each User registers, here is the general flow:
- You create a new User Form and place it on your site.
- You customize a code snippet to create the corresponding Contractor post automatically when the User Form is submitted. This ticket can be used as a general guide - https://toolset.com/forums/topic/creating-a-custom-post-type-with-cred-and-user-registration-at-the-same-time/
- Any User custom fields you would like to be able to filter with in a custom search View should be copied into the proxy post using custom fields. I am happy to help with that API if you need assistance.
- Your site visitors register using the new User Form
- The Contractor post is created automatically, the User's name is the title of the post, and the User is assigned as the post author. The proxy is now created. Your site visitors can search for Showcases using a filter of Contractor names.
- Now let's say you want to connect a Contractor and a Showcase. For this, you set up a many-to-many relationship between Contractors and Showcases. Many Contractors can be in the same Showcase, and one Contractor can be connected to many Showcases.
- If you want Contractors to be able to associate themselves to Showcases from the front-end of the site, you'll need a Relationship Form. If you want to manage the associations yourself in wp-admin, you can begin linking those posts as needed.
- The Relationship Form will link the current User's Contractor post to some Showcase. We have documentation for Relationship Forms available here: https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/
- Now you want to create a custom search View for Users to search for Showcases, and filter by Contractor name. To do this, you'll add a Post Relationship filter control to the View of Showcases. Your Users will see a filter on the front-end of the site where they search for Showcases based on which Contractor attended.

This is just a general guide. For help with any specific step, it's best to split off a different ticket to address that issue in depth. We can use this ticket as a general guideline if you'd like.

#1378419

I don't know why but this still makes no sense to me.

OK let's start from the beginning;

If I create a post form and want to 'register' a user, well I can't do that becase the user doesn't exist yet and usermeta fields don't exist in the group field for the 'contractor' CPT.

If I create a user form I can 'register' a new user but that user does not show up in the 'contractor' CPT - simply because I can't tell the user form which CPT I want it to write to.

#1378421

If I create a user form I can 'register' a new user but that user does not show up in the 'contractor' CPT - simply because I can't tell the user form which CPT I want it to write to.
Correct, Users will register with a new User form, which does not create a post. It creates a User profile only. The custom code snippet creates the Contractor post automatically when the User Form is submitted. The code snippet is the key here.

#1378437

If I add

// CRED Save Data Hook Function

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {

if ($form_data['id']==237){

// Create new entry after registration.
$my_post = array(
'post_title' => $_POST['wpcf-user-id'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => $post_id,
'post_type' => 'attendees'
);

// Insert the post into the database
wp_insert_post( $my_post );
}

To my functions.php my site errors out.

#1379347

I see a missing closing brace at the end, here's an update to your code:

// CRED Save Data Hook Function

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {

if ($form_data['id']==237){

// Create new entry after registration.
$my_post = array(
'post_title' => $_POST['wpcf-user-id'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => $post_id,
'post_type' => 'attendees'
);

// Insert the post into the database
wp_insert_post( $my_post );
}
}

Also you should check the name of the function my_save_data_action. If you have any other custom code in the site with this function name, an error will be thrown. Ideally you would change my_save_data_action to something unique in the first two lines of the code shown above...something like automatically_create_attendee would work.

If you'd like me to take a closer look in wp-admin, I'll need new login credentials. Those you provided earlier in the ticket no longer work for me.

#1379385

ok. Please add a private reply. Thanks

#1379397

Private reply fields are enabled here. Thank you.

#1379517

Okay the code sample from the other ticket must be customized to fit your site's requirements, and I don't see that you've made the necessary changes in functions.php. For example, the attendees post type slug is irrelevant for your site. You should replace it with the post type slug for Contractors - contractor - since you want to create a Contractor post:

'post_type' => 'contractor'

Also I'm not sure what you're trying to accomplish for the post_title. If you want to use the new User's ID as the Contractor post title, you would change the post title line like so:

'post_title' => $post_id,

If you want to use the value of some other field in the Form, you should update the code to point to the correct field in the $_POST object. If you can give me more information I can give more direct feedback on that.

#1379527

It stll doesn't create a user in the CPT.

#1380343

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Larry,

Christian is currently on Holiday today but will be back tomorrow to continue this ticket with you.

Thanks,
Shane

#1381143

Working now. There was a typo in the function name, maybe a copy + paste error:

add_action('cred_save_data', 'automatically_create_author',10,2);
function author($post_id, $form_data) {

...should be...

add_action('cred_save_data', 'automatically_create_author',10,2);
function automatically_create_author($post_id, $form_data) {

I made that change and saved functions.php in your child theme using the FTP plugin. Please be sure to download the latest version of that file if you need to work on it locally, otherwise you'll overwrite the changes in your next upload.

#1381235

OK! 🙂

First Thank You!

So I need to map the usermeta into contractor to do the lookup? It's not writing those details there at this point. So am I forgetting anything at this point? Sorry.

#1381485

Now that you see how it works, you can start making adjustments. First, you should probably change post_title from the User ID to be something readable. That could be the Username, or First + Last Name if you make those fields required with custom validation code, or a combination of fields. This text is what will show up in the filter for your custom search, so it should be understandable.

Then you can decide what other User attributes you want your site visitors to be able to search upon. For example, maybe you add a User field that stores a birthdate, and you want your visitors to be able to search for Showcases by Users whose birthdate is earlier than Jan 1 2000. So you would have to get the value of that User date field using get_user_meta(), then set that value in the Contractor post using update_post_meta().

I'm not sure from your description so far if that is necessary, it depends on what fields you plan to implement for search.