Hello,
what is the best practice to store post ids in a WP user profile ?
I want to show a “your latest downloads” list for logged in users in frontend…
I thought about creating an ACF textarea field and store the post ids comma seperated in it, but this solution would not be so nice for the admin – he would only see a list of ids in the backend user profile…
which would be the best way to story this information for the logged in user?
Best
P.
That would depend on what information you want to store. A relationship field on each user would probably give you the most flexibility. Then when you detect and store a value you can update the relationship field.
Be sure to use the field key for the field.
// if download detected
// get the value of the user field without formatting it
$downloads = get_field('field_012abc567', "user_{$user_id}", false);
// see if the value is already added, if not add it
if (!in_array($post_id, $downloads)) {
$downloads[] = $post_id;
// and update the field
update_field('field_012abc567', $downloads, "user_{$user_id}");
}
Hello John,
thanks a lot – I’ll check this.
I want to tore the post ids of a custom post type, so a relationship field is
the perfect solution i think.
Best
P.
Hello John,
is it possible to allow the logged in user to edit the relationships
in frontend (for example to delete some from the list) ?
Best
P.
You should be able to use an acf_form() https://www.advancedcustomfields.com/resources/acf_form/. Set the post ID when calling ACF form to "user_{$user_id}"
and that will show the groups attached to the user edit page I think.