How to set meta_query of Group cloneable fields for search
- This topic has 4 replies, 2 voices, and was last updated 8 years, 1 month ago by
Anh Tran.
-
AuthorPosts
-
September 9, 2017 at 10:49 PM #6938
yumikom
ParticipantHello,
I'd like to use field item in cloneable field for search of custom post type.
Can the condition using function get_post and `meta_query' condition be set?
Please tell me how to set meta_query.September 11, 2017 at 2:13 PM #6949Anh Tran
KeymasterHi,
When you use cloneable fields (whether it's a group or not), the data is saved as a serialized array in the database. That makes the default meta_query doesn't work when search for posts by meta value.
In this case, you need to use a plugin like SearchWP.
September 11, 2017 at 5:16 PM #6951yumikom
ParticipantHi,
Thank you very much for telling me important information.
It's being hoped to be able to use meta_query.September 21, 2017 at 6:22 PM #7018yumikom
ParticipantHi,
One of solution method most suitable for this topic was found.
For example I'd like to search for the maximum value and date period in group rows.I make readonly text field for search condition 'meta_query'.
When post save with get value(min, max for search)
and update_post_meta() in post_save ()action.A search could be sped up by readonly field for group data searches.
For example save_post action
add_action( 'save_post', 'savepost_update_meta' ); function savepost_update_meta($post_id){ // check post type or category, etc. if(get_post_type($post_id) !== 'post_type_foo') return; // get group meta data $groupRows = rwmb_meta('goup_id', $args = array(), $post_id); $priceMin = ''; $priceMax = ''; if($groupRows){ foreach($groupRows as $row){ $price = isset($row['field_price']) ? $row['field_price'] : ''; // check value if(empty($priceMin)){ $priceMin = $price; }else{ if((int)$price < (int)$priceMin) $priceMin = $price; } if((int)$priceMax < (int)$price) $priceMax = $price; } // update readonly meta field update_post_meta($post_id, 'field_readonly_text_min_price', $priceMin); update_post_meta($post_id, 'field_readonly_text_max_price', $priceMax); }else{ // delete readonly meta field when nothing group data delete_post_meta($post_id, 'field_readonly_text_min_price'); delete_post_meta($post_id, 'field_readonly_text_max_price'); } }$meta_query[] = array( 'key' => 'field_readonly_text_min_price', 'value' => $searchPrice, 'compare' => '<=', 'type' => 'NUMERIC' );Thanks anyway!
September 22, 2017 at 4:02 PM #7023Anh Tran
KeymasterThanks a lot for your solution. It's extremely helpful for other people.
-
AuthorPosts
- The topic ‘How to set meta_query of Group cloneable fields for search’ is closed to new replies.