Select Metaboxs by Attribute Flag
- This topic has 3 replies, 2 voices, and was last updated 6 years, 2 months ago by
Anh Tran.
-
AuthorPosts
-
February 13, 2019 at 6:37 PM #13305
pluginoven
ParticipantI 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?
February 14, 2019 at 10:12 AM #13318Anh Tran
KeymasterUpdated answer:
Your code is the correct one. I'm adding docs for
rwmb_get_registry
here.February 15, 2019 at 1:10 AM #13349pluginoven
ParticipantAccording 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?
February 17, 2019 at 1:35 PM #13382Anh Tran
KeymasterI'm afraid that's right.
-
AuthorPosts
- You must be logged in to reply to this topic.