Home › Toolset Professional Support › [Resolved] Multiple entries to a custom user field
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 |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
Tagged: Toolset Forms, User-registration forms
Related documentation:
This topic contains 15 replies, has 2 voices.
Last updated by Minesh 5 years, 9 months ago.
Assisted by: Minesh.
Tell us what you are trying to do?
I have a CRED form that adds post ID to a custom user field when submitted by a logged-in user. The form is essentially just a button.
The form I'm using has a hidden field that is dynamically populated using the current post ID. When a user clicks the submit button of that form, that ID gets saved to a custom field in their profile. This all works great.
The problem I'm having is that I can't save multiple entries into that field. Once a post ID gets saved then the form is clicked again on a different post, the new post ID is saved into that field instead of both. The original is removed with the new one replacing it.
The ideal solution would be to save multiple entries into the custom field so they can be recalled later within a loop and displayed on the members profile page (already built and working).
The solution has to be something similar to this but I'm having issues pulling it off.
https://toolset.com/forums/topic/multiple-values-in-custom-field/
Thanks in advance.
Hello. Thank you for contacting the Toolset support.
Well - I would like to know here that did you setup the custom field that stores the ID as the repeating field (multiple instance field)?
I set them up as repeating fields but it looks like submitting a form from the front end, doesn't actually add additional fields.
I solved the problem by using the shortcode below in my hidden field. There might be a better way of doing this but it seems to work. I haven't tested it fully though. It does save each post ID with a comma separating them.
[cred_field field="my-custom-field" force_type="field" class="form-control" output="bootstrap" value="[wpv-post-id id='$current_page'],[types usermeta='my-custom-field' separator=', ' current_user='true'][/types] "
The problem now is displaying them in a view.
I can get them to display but it shows on ALL author archives and not just on the archive where the custom field is saved.
I'm using the following shortcode in a view on the archive page but it's not working quite right. I think it's because I have so many shortcodes embedded within the others to pull the correct information.
[wpv-post-link item='[types usermeta="my-custom-field" separator=", " user_id='[get_author_id_in_archive]'][/types]']
The shortcode "get_author_id_in_archive" is from a code I found on the forum. It works well in pulling the correct author ID. When I separate it from the other shortcode, it displays the correct number. But again, I think the issue is the shortcode inside all the others.
So I've done quite a bit more work on this and I'm at the point where I can display the post IDs from the specific author. I did it using this code in my functions.php file:
function get_user_id() { $author = get_user_by( 'slug', get_query_var( 'author_name' ) ); return $author->ID; } add_shortcode( 'get_user_id', 'get_user_id' );
This creates the shortcode:
[get_user_id]
It does a good job of grabbing the author ID using the slug.
I then can display the post numbers from that author using this shortcode:
[types usermeta='my-custom-field' separator=', ' user_id='[get_user_id]'][/types]
It displays the numbers separated by a comma.
I would like to use this shortcode to take those post IDs and show the post link but it doesn't seem to work. What am I missing or am I approaching this the wrong way?
[wpv-post-link item="[types usermeta='my-custom-field' separator=',' user_id='[get_user_id]'][/types]"]
Seems like I'm close but who knows 🙂
Thanks for your time and help!! It's very much appreciated.
Well - I would like to understand your exact requirement first that will help me to guide you in the right direction.
- you have custom user field that used to store the post IDs - correct?
- You want to display post title with link for those stored post ID values to user custom field - correct?
- Where exactly you want to display this?
Can you please share problem URL and access details so after reviewing your structure I will be in a better position to share the solution.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
Well - I've added the following code to Toolset's custom code section:
=> hidden link
// to get author story IDs add_filter( 'wpv_filter_query', 'func_get_author_story_ids', 10, 3 ); function func_get_author_story_ids( $query_args, $setting,$view_id ) { $post_ids = array(); if($view_id == 152){ $author = get_user_by( 'slug', get_query_var( 'author_name' ) ); $post_ids = get_user_meta($author->ID,'wpcf-favorite-stories'); if( !empty($post_ids)){ $query_args['post__in'] = $post_ids; }else{ $query_args['post__in'] = 0; } } return $query_args; } // to get favorite author IDs add_filter( 'wpv_filter_query', 'func_get_fv_author_ids', 10, 3 ); function func_get_fv_author_ids( $query_args, $setting,$view_id ) { $post_ids = array(); if($view_id == 153){ $author = get_user_by( 'slug', get_query_var( 'author_name' ) ); $author_ids = get_user_meta($author->ID,'wpcf-favorite-authors'); if( !empty($author_ids)){ $query_args['include'] = $author_ids; }else{ $query_args['include'] = 0; } } return $query_args; } // to add multiple entries to favorite authors field add_action('cred_save_data', 'func_save_author_ids',10,3); function func_save_author_ids($user_id, $form_data){ // if a specific form if ($form_data['id']==142){ $current_author_ids = get_user_meta( $user_id,'wpcf-favorite-authors'); if(!in_array($_POST['wpcf-favorite-authors'],$current_author_ids)){ add_user_meta($user_id,'wpcf-favorite-authors',$_POST['wpcf-favorite-authors'],false); } } }
I've also modified the form a bit:
=> hidden link
I've also changed the view's loop editor output here:
=> hidden link
I can see now everything is displayed correctly:
=> hidden link
I can see now everything looks OK, could you please confirm.
Looks great!
Thanks so much for your help. I didn't realize that was going to be so complicated.
My issue is resolved now. Thank you!
Hi Minesh,
Something is still wrong. It looks like all the author pages are showing the information for the logged in user INSTEAD of the information for that author.
In other words, if you go here to the two pages below, you'll notice they have the same information for the favorite stories and favorite authors. I think it's showing the favs for the logged-in user and NOT the author.
Let me know what you think:
hidden link - Should NOT be showing any information for favorites. This user has nothing saved.
hidden link - This user (me) should have 2 favorite stories and shows 3.
Thanks!
Can you please share access details again so I can check whats going wrong there.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
Ok - thank you for sharing access details.
I've adjusted the code and added the following shortcode to custom code section of Toolset:
function func_get_author_story_ids($atts) { $author_id = get_queried_object()->ID; $story_ids = get_user_meta($author_id,'wpcf-favorite-stories'); if(!empty($story_ids)){ return join(",",$story_ids); }else{ return 0; } } add_shortcode("get_author_story_ids", "func_get_author_story_ids");
I've adjusted the view to use the above shortcode as shortcode attribute:
[wpv-view name="favorite-story-list-for-archive" ids="[get_author_story_ids]"]
I've also adjust the code for following query hook:
add_filter( 'wpv_filter_user_query', 'func_get_fv_author_ids', 10, 3 ); function func_get_fv_author_ids( $query_args, $setting,$view_id ) { $post_ids = array(); if($view_id == 153){ $author_id = get_queried_object()->ID; $author_ids = get_user_meta($author_id,'wpcf-favorite-authors'); if( !empty($author_ids)){ $query_args['include'] = $author_ids; }else{ $query_args['include'] = array(0); } } return $query_args; }
I can see now on both author profile the content displayed is accurate:
=> hidden link
=> hidden link
Can you please confirm.
Looks like it's working great! I'm going to test a bit and then close the ticket. Thanks for the help!!
Your welcome 🙂
Could you please confirm - its working or not 🙂
Looks like it works great Minesh! I had to redo a couple other views I had but it wasn't an issue. Thanks for the help!!