简体   繁体   中英

WP-Advanced-Search using ACF multi-select field

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM