dtrpv60860 2019-06-26 04:01
浏览 98

WP-Advanced-Search使用ACF多选字段

Can't figure out how to use an ACF custom field with multiple values in WP Advanced Search form.

I'm working on a faceted search using Advanced Custom Fields and WP Advanced Search, and most problems I've run into I've been able to figure out on my own or eventually find someone doing something similar.

So far however, I can't find any example of how to use a custom field with multiple values. I don't know how to set up my WPAS form to address each individual value, or if it's even possible.

This custom field only stores a single value for post, in which case meta_value in wp_postmeta is just the expected string ie. Condo, Townhouse, Detached, etc. This is working fine...

$args['fields'][] = array(  'type'      => 'meta_key',
                            'meta_key'  => 'property_style',
                            'format'    => 'multi-select',
                            'compare'   => 'LIKE',
                            'data_type' => 'ARRAY<CHAR>',
                            'relation'  => 'OR',
                            'label'     => 'Property Style',
                            'values'    =>  array(
                                                'Condo Townhouse'    => 'Condo Townhouse',
                                                'Condo Apartment'    => 'Condo Apartment',
                                                'Detached'           => 'Detached',
                                                'Semi-Detached'      => 'Semi-Detached',
                                                'Freehold Townhouse' => 'Freehold Townhouse',
                                                'Link'               => 'Link',
                                                'Other'              => 'Other'
                                            ) 
                    );

This ACF field accepts multiple values. Storing them is not an issue. The multi-select generated by ACF works fine, as does my importer, but I can't seem to understand how to set up the 'data_type' and 'compare' values to actually address those individual values... My theory is 'data_type' needs to reflect the structure of the array represented by the string stored in meta_value (see question) but I can't quite figure it out...

$args['fields'][] = array(  'type'      => 'meta_key',
                            'meta_key'  => 'basement',
                            'format'    => 'multi-select',
                            'compare'   => 'LIKE',
                            'data_type' => 'ARRAY<CHAR>',
                            'relation'  => 'OR',
                            'label'     => 'Basement',
                            'values'    => array(
                                                'Apartment'          => 'Apartment',
                                                'Crawl Space'        => 'Crawl Space',
                                                'Finished Walk Out'  => 'Finished Walk Out', 
                                                'Finished'           => 'Finished', 
                                                'Full'               => 'Full', 
                                                'Half'               => 'Half', 
                                                'None'               => 'None',
                                                'Other'              => 'Other',
                                                'Partial Basement'   => 'Partial Basement', 
                                                'Partially Finished' => 'Partially Finished', 
                                                'Separate Entrance'  => 'Separate Entrance',
                                                'Unfinished'         => 'Unfinished',
                                                'Walk Out'           => 'Walk Out', 
                                                'Walk-Up'            => 'Walk-Up'
                                             ));

The meta data for the multi-select field looks like this, if it helps:

--------------------------------------------------------------------------------------------+
| meta_id | post_id | meta_key  | meta_value                                                |
+---------+---------+-----------+-----------------------------------------------------------+
|  150263 |    5286 | _basement | field_5cf59fb01a564                                       |
|  150262 |    5286 | basement  | a:1:{i:0;s:9:"Apartment";}                                |
|  150025 |    5276 | _basement | field_5cf59fb01a564                                       |
|  150026 |    5276 | basement  | a:1:{i:0;s:9:"Apartment";}                                |
|  149792 |    5255 | _basement | field_5cf59fb01a564                                       |
|  149793 |    5255 | basement  | a:2{i:0;s:8:"Finished";i:1;s:4:"Full";}                   |
|  149531 |    5238 | _basement | field_5cf59fb01a564                                       |
|  149532 |    5238 | basement  | a:2{i:0;s:9:"Apartment";i:1;s:17:"Separate Entrance";}    |
|  149278 |    5220 | _basement | field_5cf59fb01a564                                       |
|  149279 |    5220 | basement  | a:2{i:0;s:9:"Apartment";i:1;s:8:"Finished";}              | 
|  149023 |    5199 | _basement | field_5cf59fb01a564                                       |
|  149024 |    5199 | basement  | a:2{i:0;s:8:"Finished";i:1;s:17:"Separate Entrance";}     |

I know I'm doing it wrong and can't find even a hint of info on what I should be doing instead. I'm not sure WP Advanced Search is even still in development but it's still functioning with the latest release of WP so not really my concern.

  • 写回答

1条回答 默认 最新

  • duanpi5733 2019-06-26 06:26
    关注

    I'm not exactly sure what I did differently but this seems to have worked... I think I was right from the start but maybe had something else causing problems elsewhere...

    Anyway it's the same syntax as a regular ACF custom field with a single value.

        $args['fields'][] = array(  'type'                  => 'meta_key',
                                    'meta_key'              => 'basement',
                                    'format'                => 'multi-select',
                                    'compare'               => 'LIKE',
                                    'data_type'             => 'ARRAY<CHAR>',
                                    'relation'              => 'OR',
                                    'label'                 => 'Basement',
                                    'values'                => array(
                                                                    'Apartment' => 'Apartment',
                                                                    'Crawl Space' => 'Crawl Space',
                                                                    'Finished Walk Out' => 'Finished Walk Out',
                                                                    'Finished' => 'Finished',
                                                                    'Full' => 'Full',
                                                                    'Half' => 'Half',
                                                                    'None' => 'None',
                                                                    'Other' => 'Other',
                                                                    'Partial Basement' => 'Partial Basement',
                                                                    'Partially Finished' => 'Partially Finished',
                                                                    'Separate Entrance' => 'Separate Entrance',
                                                                    'Unfinished' => 'Unfinished',
                                                                    'Walk Out' => 'Walk Out',
                                                                    'Walk-Up' => 'Walk-Up'
                                                                )
                                                            );
    

    EDIT: I figured out why I thought it was wrong. It started working when I was working with values input through the back end. The values I'm generating in my importer were missing a colon.

    ACF generated serialized array: a:2:{i:0;s:9:"Apartment";i:1;s:11:"Crawl Space";}

    My importer's "serialized array": a:2{i:0;s:8:"Finished";i:1;s:4:"Full";

    (No colon after "a:2")

    Maybe if I'd actually serialized an array like a normal human rather than doing it all through string manipulation like a weirdo, I would never have spent all this time trying to fix working code. Let this be a lesson to us all, but mostly to me.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看