• Resolved tychow

    (@tychow)


    Hi, I wish to check for existing groups with specific names, and I think I can do that using the https://publishpress.com/knowledge-base/pp_get_groups/ funtion.

    But if a group doesn’t exist, I wish to create one with permissions added to it already:
    In my case I wish to add: Add Specific Permissions -> Post type: Post -> Operation: View -> Adjustment: Enable -> Qualification -> Categories: Selected Categories -> All Statuses -> and then select a specific category.

    I have found this site but it doesn’t list my case I think.
    https://publishpress.com/knowledge-base/ppc-assign-exceptions/

    Using the developer API

    How can I achieve this? Hope to hear from you.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Author Richa Ferry Setyawan

    (@aakricha)

    Hi @tychow 👋
    Thank you again for your patience — and sorry for the delay in replying. We really appreciate you waiting for us.

    About your question — you can check whether a group exists using the Permissions API. And if the group doesn’t exist, you can create it and assign permissions immediately with the configuration you described:

    Add Specific Permissions → Post type: Post → Operation: View → Adjustment: Enable → Qualification → Categories → All Statuses → select a specific category

    Below is a complete working example. This version will check the group by name, create it if it doesn’t exist, and then assign the permissions:

    <?php

    try {
    // Try to get the group by name
    $group = pp_get_group_by_name("Sample Group", 'pp_group');

    // If no group found, create one
    if (!$group) {
    $group_id = pp_create_group([
    'group_name' => 'Sample Group',
    'group_description' => 'Automatically created group'
    ]);

    if (!$group_id) {
    exit("❌ ERROR: Failed to create group\n");
    }

    echo "✅ SUCCESS: Group created with ID {$group_id}\n";
    $group = get_post($group_id); // Load group object
    }

    // Get the target category (example: slug = sample-category)
    $term = get_term_by('slug', 'sample-category', 'category');

    // Assign permission rules
    $args = [
    'operation' => 'read',
    'mod_type' => 'additional',
    'for_item_source' => 'post',
    'for_item_type' => 'post',
    'via_item_source' => 'term',
    'via_item_type' => 'category',
    'item_id' => $term->term_taxonomy_id,
    ];

    $agents = [];
    $agents['item'][$group->ID] = true;

    ppc_assign_exceptions($agents, 'pp_group', $args);
    echo "✅ SUCCESS: Permissions assigned successfully to group\n";

    } catch (Exception $e) {
    exit('❌ ERROR: ' . $e->getMessage());
    }

    Short explanation

    • Post type: Post -> ‘for_item_type’ => ‘post’,
    • Operation: View -> ‘operation’ => ‘read’,
    • Adjustment: Enable -> ‘mod_type’ => ‘additional’,
    • Categories -> ‘via_item_type’ => ‘category’,
    • select a specific category -> ‘item_id’ => $term->term_taxonomy_id,

    Just let us know — we’re always glad to help! 🙌
    Wishing you a wonderful day ahead!

Viewing 1 replies (of 1 total)

The topic ‘API | create group with specific permission’ is closed to new replies.