0

I'm building a medical lab site, I'd like to link a custom post type - results to users. How can I link results CPTs to a user?

The idea is to be able to associate multiple test results to a single user/member. Currently using woocommerce membership to power the membership.

Please, any idea(s) or just point me in the right direction would be helpful. Thank you in advance!

2
  • Use a custom field (meta key/value) that holds the user ID. Each Results post will have a user ID and one user ID can be applied to many results posts. wordpress.org/support/article/custom-fields
    – jdm2112
    Commented Jul 29, 2021 at 13:08
  • Aside: I'm not sure what the UK's healthcare privacy law is like, but in the USA, I would be very careful if not outright skeptical about any project wherein patients' medical data is stored on a WordPress site, as HIPAA law places a huge responsibility on fully securing such sites to very stringent standards with violation fees large enough to tank a small medical company (capped at $1.8 million/year). Even the lowest level violations can result in prison time. All of that terrifies me enough that I would never consider any variety of patient portal with WordPress as a live CMS.
    – bosco
    Commented Aug 1, 2021 at 20:45

1 Answer 1

0

If I can understand what you are looking for, you can create a simple function, that will fire when a new of the CPT is created and update a post_meta eg: "user_controler" for that.

Later, you will need to filter, the posts on the post.php, to show posts only related to the current user.

add_action("save_post", function ($post_ID, $post){
    $post = get_post($post_ID);
    $user = get_curent_user();

    if($post->post_type === "my_cpt" && !get_post_meta($post_ID, "my_meta_key")){
        update_post_meta($post_ID, "my_meta_key", $user->key);
    }
    //$user->key matches what you want to kip as link.
});

To filter the posts on the post.php page you can:

add_filter( 'posts_results', 'my_posts_results_filter' );
    
function my_posts_results_filter( $posts ) {
    global $my_global_condition;
    $user = get_curent_user();
    $screen = get_current_sreen();

    $filtered_posts = array();
        
    foreach ( $posts as $post ) {
        if (
        $post->post_type === "my_cpt" && 
        $screen->ID === $my_cpt && 
        get_post_meta($post_ID, "my_meta_key")=== $user->key
        ) {
                // It's allow :).
                $filtered_posts[] = $post;
        }
    }
    return $filtered_posts;
}

You can also use post_where filter.

You can also filter post simple base on the author property.

With 💕.

1
  • Hey guys, thanks for all your responses. I've tried relentlessly for one week to get this working, no luck! What I'm trying to achieve is: 1. Create a custom post type (done). 2. Have the ability to associate this post type to a specific user from the post creation screen (i.e, from a dropdown list of all registered users or members since this is a membership site). 3. Be able to display this associated post on the users' dashboard. That is, display only the associated or linked CPT to the user it was associated with. Please, how do you achieve something like this?
    – jeno
    Commented Aug 18, 2021 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.