Skip Navigation

[Resolved] Custom Search For Users View Alternative

This support ticket is created 6 years, 3 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
- 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: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by Luo Yang 6 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1078810

Amr

I am trying to use custom search for "Users Views" to filter out profiles based on some user custom fields. I discovered that that is not possible at this time.

So the solution would be to create a custom post type and use custom fields to store user related info. However, we need to be able automatically create the CPT record for a user after a user profile is created and assign the CPT post author to that newly created user. Is there a way to do that using CRED "cred_save_data" hook ?

So a user would register on the site by using a CRED user form. As soon as the user profile is created, we need to automatically create a CPT post using the "cred_save_data" hook and assign that CPT post to the newly created user.

Can you please suggest the code to do that.

This will obviously be a temporary solution until Views start supporting custom search for Users Views which I believe is in the pipeline of requested features.

Thank you.

#1079498

Hello,

Yes, you are right, it needs some custom PHP codes with "cred_save_data" hook.

For example, you can try these:
1) Create a Toolset form for creating user

2) after submit the form, use action hook "cred_save_data" hook to trigger a PHP function
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

3) In this PHP function, create a new post, and the post author is the new user
https://developer.wordpress.org/reference/functions/wp_insert_post/

See similar thread here:
https://toolset.com/forums/topic/create-a-new-ctp-post-when-a-user-is-created-with-user-registration-a-cred-form/#post-406959

#1080313

Amr

Thank you for the reply and suggestion. In this code:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($user_id, $form_data)
{
// Change your CRED Form ID
if ($form_data['id']==9999){

$new_post = array(
'post_type' => 'YOUR-POST-TYPE',
'post_status' => 'publish',
'post_title' => 'YOUR-POST-TITLE',
);
wp_insert_post($new_post);
}
}

How can we retrieve some user related data that was part of the CRED User form and pass it to the function so we can store that data in the new post we are creating here like the newly created user id, login id and user first and last name as well as other user custom fields that were part of the CRED user registration form ?

Also, when an CRED user form that updates the user info like email address or user last name is submitted, I need to do the same but this time do an update of the the related CPT post that we created. This way, the data in the user profile stays in sync with the related CPT post. Do I use wp_update_post ?

Thanks

#1081046

Q1) How can we retrieve some user related data that was part of the CRED User form and pass it to the function

As you can see you can get the new user's ID with var $user_id, with the user's ID, you can get the user other information, for example get_user_by():
https://developer.wordpress.org/reference/functions/get_user_by/

Q2) Do I use wp_update_post ?
Yes, you can use function wp_update_post() to update the post information
https://developer.wordpress.org/reference/functions/wp_update_post/

If you need to update custom fields value, you will try another function update_post_meta:
https://developer.wordpress.org/reference/functions/update_post_meta/

#1090308

Amr

Thank you for your reply and answers. I do however have a related question.

At registration time, using a CRED user form, we capture a user spoken languages using Checkboxes custom field. We then pass the selected values to the related CPT record that we create on the fly using the "cred_save_data" hook. In the CPT record, we have a similarly defined Checkboxes custom field to store the languages. The challenge we are facing is that, we are passing the values of the "user language meta field" to the "CPT language meta field" exactly "as is" using the following:

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

if ($form_data['id']==xxx)
{
$user = get_user_by( 'id', $post_id );
$userlogin = $user->user_login;
$useremail = $user->user_email;
$languagestaught = get_user_meta($post_id, 'wpcf-languages-taught', true);

$new_post = array(

'post_type' => 'profile-archive',
'post_status' => 'publish',
'post_title' => $userlogin,
'post_author' => $post_id,
'meta_input' => array(
'wpcf-user-languages-taught' => $languagestaught,
),
);

wp_insert_post($new_post);
}
}

Because of the way Types store checkboxes in a serializes array, when passing the values selected from one checkboxes field to another checkboxes field, it does not work event if the field definition values are the same. See:

This the value that we store in the "user meta language checkboxes field":
a:2:{s:64:"wpcf-fields-checkboxes-option-88f76ae1b2b521ccc00621105a81acac-1";a:1:{i:0;s:6:"Arabic";}s:64:"wpcf-fields-checkboxes-option-7c065902a604d2aa9fc72a0914116677-1";a:1:{i:0;s:4:"Dari";}}

This the value that is expected to be stored in the "CPT meta language checkboxes field" for it to work:
a:2:{s:64:"wpcf-fields-checkboxes-option-c954607b4f3c4a21a3bb8f53e425673d-1";a:1:{i:0;s:6:"Arabic";}s:64:"wpcf-fields-checkboxes-option-4b696a1d773874edd1117e2e87e775b9-1";a:1:{i:0;s:4:"Dari";}}

When we straight copy the value of the "user meta language checkboxes field" into the "CPT meta language checkboxes field", it does not work since the wpcf-fields-checkboxes-option- are different.

Is there a way to read the array values from the "user meta language checkboxes field" and write those same values to the "CPT meta language checkboxes field" when a CRED user form is submitted.

Thank you.

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/split-custom-search-for-users-view-alternative/

#1090504

I assume the original question of this thread has been resolved, for other new question please check the new thread here:
https://toolset.com/forums/topic/split-custom-search-for-users-view-alternative/