Custom Fields
Re-Envisioned
Support MB Custom Taxonomy Select a default term based on user

This topic contains 3 replies, has 2 voices, and was last updated by  Anh Tran 3 weeks, 4 days ago.

  • Creator
    Topic
  • #16572
    Resolved
    brandonjp
    Participant

    I’m having trouble setting a default value & hoping you can guide me:

    • I have a Custom Post Type called “Job” with a taxonomy select field called “job_manager”
    • I have a Custom Taxonomy called “Manager” with a user select field called “wp_user”

    When creating a new Job, I want to select the default value for “job_manager” to be the currently logged in user, but so far I’m having trouble understanding how the terms are attached and how to set the default term.

    Basically, what value do I want from the Manager term? And how do I assign it as default

    I’ve written code based on this guide: https://docs.metabox.io/edit-meta-boxes/ to modify the meta box.

    add_filter('rwmb_meta_boxes', 'select_jobmanager_edit_meta_boxes', 20);
    function select_jobmanager_edit_meta_boxes($meta_boxes)
    {
        // Loop through all metaboxes
        foreach ($meta_boxes as $k => $mb) {
            if (isset($mb['id']) && 'job-personnel' == $mb['id']) {
                // Loop through all fields
                foreach ($mb['fields'] as $j => $field) {
                    // Find the manager taxonomy field
                    if ('job_manager' == $field['id']) {
                        // Here I'll get_current_user then find the "manager" term
                        // using get_terms/'meta_query' with matching the "wp_user"
                        // But what value do I need from the "manager" term & 
                        // and what property do I set on the field
                        $meta_boxes[$k][$j]['std'] = ['44'];
                    }
                }
        }
    }
    
    // Return edited meta boxes
    return $meta_boxes;
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #16588

    Anh Tran
    Keymaster

    Hi,

    I see you have 2 relationships: job (post type) < -> manager (post type) and manager (post type) < -> user.

    Your code targets to the 1st relationship, which means you will be able to set the default manager (post type) for a job (post type).

    Why don’t you simplify the logic with only one relationship: job (post type) < -> user? With that you don’t need the taxonomy, and thus, can set 'std' => get_current_user_id() in the field easily.

    #16608

    brandonjp
    Participant

    Yes, that’s what I was originally doing. However, not every Manager has a user account.

    (Plus I’m using Admin Columns Pro and the displaying/filtering of posts by term is much more friendly for my users – https://i.imgur.com/NF1NfLA.png )

    #16626

    Anh Tran
    Keymaster

    If so, I’d suggest creating a function like this:

    function get_default_manager() {
        $default_user = get_current_user_id(); // Or any user ID you want to set as default
    
        $manager = rwmb_meta( 'manager', ['object_type' => 'term'], $default_user );
        return $manager ? $manager->term_id : 0;
    }

    Then in your code, set 'std' => get_default_manager().

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.