pmpro_updateMembershipCategories

Pmpro_updateMembershipCategories() ensures that all those and only those categories given are associated with the given membership level.

Description

pmpro_updateMembershipCategories( $level, $categories ); 

$level is a valid membership level ID or name $categories is an array of post category IDs

Return values: Success returns boolean true. Failure returns a string containing the error message.

Parameters (2)

0. $level
The level.
1. $categories
The categories.

Usage

  1. if ( !function_exists( 'pmpro_updateMembershipCategories' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/includes/functions.php'; 
  3.  
  4. // The level. 
  5. $level = null; 
  6.  
  7. // The categories. 
  8. $categories = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = pmpro_updateMembershipCategories($level, $categories); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/functions.php  
  1. function pmpro_updateMembershipCategories($level, $categories) 
  2. global $wpdb; 
  3.  
  4. if(!is_numeric($level)) 
  5. $level = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_membership_levels WHERE name = '" . esc_sql($level) . "' LIMIT 1"); 
  6. if(empty($level)) 
  7. return __("Membership level not found.", 'paid-memberships-pro' ); 
  8.  
  9. // remove all existing links... 
  10. $sqlQuery = "DELETE FROM $wpdb->pmpro_memberships_categories WHERE `membership_id` = '" . esc_sql($level) . "'"; 
  11. $wpdb->query($sqlQuery); 
  12. if($wpdb->last_error) return $wpdb->last_error; 
  13.  
  14. // add the given links [back?] in... 
  15. foreach($categories as $cat) 
  16. if(is_string($r = pmpro_toggleMembershipCategory( $level, $cat, true))) 
  17. //uh oh, error 
  18. return $r; 
  19.  
  20. //all good 
  21. return true;