[MEMBERSHIP] migrate posts from one membership id to another

I'm trying to move posts and attachments from the default membership into a newly created membership - but via the gui i can only update 20 at a time, so i was trying to locate the settings via the database, and i thought the settings for each page and attachment would be found in the post_meta table, but it doesn't seem so (at least not with the meta_key 'membership_id'.

So I just wanted to know how I can lookup the membership protection rules in the database, or alternatively mass update through the gui :slight_smile:

  • Adam Czajczyk

    Hi Dennis

    I hope you're well today and thank you for your question!

    I'm not sure how exactly the memberships information (which membership protects which post/page) are stored in the database but from what I can see after a brief look into the db, it's a bit more complex than just a single meta value. We'll need some help from our developers on this so I have already forwarded this question to them and they'll check if there's some reasonable way to achieve what you need.

    Please keep an eye on this ticket and we'll update you here as soon as we receive some feedback from them.

    Kind regards,
    Adam

  • Adam Czajczyk

    Hello again Dennis

    There's been two solutions suggested. One is to increase number of items on a Protection Rules pages and it can be achieved by this code:

    add_filter( 'rule_items_per_page', function( $per_page ){ 
    
        if ( function_exists( 'get_current_screen' ) && 'membership-2_page_membership2-protection' == get_current_screen()->id ) {
            return 50;
        }
    
        return $per_page;
    } );

    The number in return 50 line is a number of items that should be displayed per page (50 in this case but you may increase it further. You can add this code to the "functions.php" file of your current theme.

    The other solution is to copy from one membership to another by code:

    function wpmudev_ms_get_rules_hierarchy(  $membership) {
            $rules             = array();
            $cache_key     = MS_Helper_Cache::generate_cache_key( 'ms_model_membership_get_rules_hierarchy_' . $membership->subscription_id . '_' . $membership->id );
            $results     = MS_Helper_Cache::get_transient( $cache_key );
            if ( $results ) {
                $rules = $results;
            } else {
                $rule_types     = MS_Model_Rule::get_rule_types();
                $subscription     = MS_Factory::load( 'MS_Model_Relationship', $membership->subscription_id );
    
                foreach ( $rule_types as $rule_type ) {
                    $rule         = $membership->get_rule( $rule_type );
    
                    if ( $rule->rule_type != $rule_type ) {
                        // This means that the $rule_type was not found...
                        continue;
                    }
    
                    // Sometimes the $subscription->id can be 0, which is intentional:
                    // This is the case when the membership was auto-assigned to guest
                    // or default membership.
                    $rule->_subscription_id = $subscription->id;
    
                    $rule->membership_id     = $membership->id;
                    $rules[ $rule_type ]     = $rule;
                }
                MS_Helper_Cache::query_cache( $rules, $cache_key );
            }
    
            return $rules;
        }
    
    add_action( 'load-membership-2_page_membership2-protection', function(){
    
        // for source membership id = 52
        // and target membership id = 319
        // use the url : site.com/wp-admin/admin.php?page=membership2-protection&source=52&target=319
    
        $source_membership_id     = isset( $_GET['source'] ) && is_numeric( $_GET['source'] ) ? (int) $_GET['source'] : false;
        $target_membership_id     = isset( $_GET['target'] ) && is_numeric( $_GET['target'] ) ? (int) $_GET['target'] : false;
    
        if ( ! $source_membership_id || ! $target_membership_id ) {
            return;
        }
    
        $source_membership         = MS_Factory::load( 'MS_Model_Membership', $source_membership_id );
        $target_membership         = MS_Factory::load( 'MS_Model_Membership', $target_membership_id );
        $rules                     = wpmudev_ms_get_rules_hierarchy( $source_membership );
    
        if ( empty( $rules ) ) {
            return;
        }
    
        foreach ( $rules as $rule_type => $rule ) {
            $target_membership->set_rule( $rule_type, $rule );
        }
    
        $target_membership->save();
    
    } );

    This code can also be added to the "functions.php" file of your current theme and once it's there, call out your site in browser like this:

    site.com/wp-admin/admin.php?page=membership2-protection&source=123&target=456

    where 123 number for source parameter is the current membership ID (the one you wish to move from) and the 456 for target parameter is the ID of a membership you wish to move your posts to, so you'll want to replace these IDs with those matching your site memberships.

    I would also strongly recommend taking a full backup of the site before giving it a go as it's not a "standard" solution so it's better to take some safety precautions just in case.

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.