How to set meta_query of Group cloneable fields for search

Support MB Group How to set meta_query of Group cloneable fields for search

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #6938
    yumikomyumikom
    Participant

    Hello,
    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.

    #6949
    Anh TranAnh Tran
    Keymaster

    Hi,

    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.

    #6951
    yumikomyumikom
    Participant

    Hi,

    Thank you very much for telling me important information.
    It's being hoped to be able to use meta_query.

    #7018
    yumikomyumikom
    Participant

    Hi,

    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!

    #7023
    Anh TranAnh Tran
    Keymaster

    Thanks a lot for your solution. It's extremely helpful for other people.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘How to set meta_query of Group cloneable fields for search’ is closed to new replies.