Custom Fields
Re-Envisioned
Support General Select Metaboxs by Attribute Flag

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

  • Creator
    Topic
  • #13305
    Resolved
    pluginoven
    Participant

    I am attempting to automatically create filters on the front end based on some back-end metaboxes. facet-wp is not being used as it’s too much overhead for this simple use.

    The fields to be used as filters are flagged as as such using is_filter during the field creation like so:

    'fields' => array(
        array(
            'name'  => __('Gender', 'text-domain'),
            'desc'  => '',
            'id'    => 'user_gender',
            'type'  => 'select',
            'options'    => array(
                            'Male'      => __('Male', 'text-domain'),
                            'Female'    => __('Female', 'text-domain'),
                            'Diverse'   => __('Diverse', 'text-domain'),
                        ),
            'attributes' => array(
                            'required'  => true,
                            'mod_lock'  => true,
                            'is_filter' => true,
                        ),
       ),
    )

    Is there a recommended way of selecting all fields that have is_filter set?
    This following is working, but maybe there is a better method?

    $meta_box_objects = rwmb_get_registry( 'meta-box' )->all();
    foreach ( $meta_box_objects as $meta_box_object ) {
        $fields = $meta_box_object->fields;
        foreach($fields as $field){
            if(!empty($field['attributes']['is_filter'])){
                //filter field found
                $content .= $field['id'].'<br/>';
            }
        }
    }

    rwmb_get_registry is not very well documented, so maybe there is a more efficient method?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #13318

    Anh Tran
    Keymaster

    Updated answer:

    Your code is the correct one. I’m adding docs for rwmb_get_registry here.

    #13349

    pluginoven
    Participant

    According to the new documentation (thank you, for this) I should be able to simply create an has_filter argument for the $meta_boxes object like so:

    $meta_boxes[] = array(
                'id'         => 'gender_box',
                'title'      => __('Gender', 'text-domain'),
                'post_types' => 'some_post_type',
                'context'    => 'normal',
                'priority'   => 'high',
                'has_filter'  => true,
    
                'fields' => array( 
                    ...
                ),
    )

    and then grab only the meta_boxes that contain this new argument using the get_by method:

    $args = [
        'has_filter'   => true,
    ];
    $meta_box_objects = rwmb_get_registry( 'meta-box' )->get_by( $args );

    All fields in each returned meta_box will still need to be looped through in order to check for the specific attribute. But at least this way I do not need to loop all meta_boxs. However, there is no way to directly get only the fields that have a specific attribute set, correct?

    #13382

    Anh Tran
    Keymaster

    I’m afraid that’s right.

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

You must be logged in to reply to this topic.