Link custom post type to users membership

admin2025-01-07  7

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!

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!

Share Improve this question asked Jul 29, 2021 at 12:35 jenojeno 486 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262965a853.html

最新回复(0)