Create a Group via PHP
-
Hi,
I’m interested in creating groups on the fly..any guidance concerning this as regard what needs to be setup in the db?Many thanks
-
for groups
Table – wp_options
key name – rpg_groups
value – an array of the groups and names eg a:3:{s:6:”group1″;s:8:”Teachers”;s:6:”group2″;s:8:”Students”;s:6:”group3″;s:7:”Parents”;}for users
Table – wp_usermeta
user_id – the id of the user
meta_key – ‘private_group’
value – a single group eg ‘group1’ or an array of groups ‘*’ separated eg ‘*group1*group4*’for forums
table – wp_postmeta
post_id – the forum id
meta_key – ‘_private_group’
Value – the group namefor forums with multiple groups, there are multiple instances of the postmeta entries.
Thank you for the info.
I managed to figure all that out before seeing your reply ..took me a while ..but great for my general learning LOL!!!Basically my setup has classes that require private forums.
I have managed to achieve the following dynamically:
1. create a new group
2. create a new forum
3. assign the group to the forum
4. when users successfully sign up to the class..they are also automatically added to the group.Today, i have attempted the following.
To assign another class to the same forum.Run into a problem..was hoping you could shed any light and maybe i’ll figure out if I have bug somewhere:
When updating the content of the forum post (basically adding classes information) it seems to wipe out the groups checked for that forum???
I’m using wp_update_post() and only passing the id and the updated content..nothing else…
I would assume i have bug somewhere in my code as can’t see it related to groups but just asking in case…Just to add:
when i first create the group/forum and assign, I have the following example entry in the database table wp_postmetameta_id (11887), post_id (4321), meta_key (_private_group), meta_value (Group45)
when I add information later by only updating the forum post content, the entry is gone – using wp_update_post()
found this…just fishing without a rod though π
But, as @rast said, it is very probably that your problem is with some of these actions hooked by third party code (from another plugin or from the theme). These actions are triggered by wp_update_post() and it is commonly used to update post meta fields, specially save_post action. It is very probably that when these actions are triggered without meta information, like your code does, the third party code deletes the meta fields.
from http://wordpress.stackexchange.com/questions/227824/wp-update-post-deletes-post-meta-in-cpt
Now with forums you have multiple entries, so the database can have multiple lines
eg if you forum ID was 2927, and you have set it for group1 and group3, you would have two entries in the postmeta table
meta_id Post_id Meta_key Meta_value
16263 2927 __private_group group1
17652 2927 __private_group group3Try unpicking the code below to see how the save function works for this
/* Get post metadata for the custom field key 'group'. */ $meta = (array)get_post_meta( $post_id, '_private_group', false ); /* Check if the group was selected. */ if ( !empty ($_POST[$group] )){ /* If selected and already saved, continue looping through the roles and do nothing for this role. */ if ( in_array( $group, $meta ) ) continue; /* If the group was selected and not already saved, add the group as a new value to the 'group' custom field. */ else add_post_meta( $post_id, '_private_group', $group, false ); } /* If role not selected, delete. */ else { delete_post_meta( $post_id, '_private_group', $group ); } } // End loop through site's groupsOnly just read your last one after I had posted.
Suggest you try using update_post_meta rather than update post
Thanks for your prompt reply
But i want to update the post content (post_content) in wp_posts…
anyhow i replaced wp_update_post() with:
global $wpdb;
$query = “UPDATE “.$wpdb->prefix.”posts SET post_content='”.$content.”‘ WHERE ID = ‘”.$existing_forum_id.”‘”;
$wpdb->query($query);and it worked fine π
thanks again
ok, no problem
The topic ‘Create a Group via PHP’ is closed to new replies.