0

Problem: ACF field has array of IDs (stored as strings) not obeying WPQuery (custom query)

Data of post_meta.meta_key = 'EventTopicId'

a:3:{i:0;s:2:"10";i:1;s:2:"32";i:2;s:2:"30";}

I have several other meta_key fields that, combined, create a filter for WPQuery. (e.g. EventTopicId + CategoryId + ...)

Id's are passed as array. When using 'IN' operator the query returns 0 results. This is the closest I've found to what I believe I need. Array into Comma separated array in PHP.

$metaSearchQuery[] =[
   'key'          => 'EventTopicId',
   'value'        => $eventTopicId,
   'compare'      => 'IN'
];

The problem is that my MetaQuery is an AND relationship (filter1 + filter2 etc). The assumption was 'IN' would do the job on ACF field... But if I pass an array of 10 $eventTopicId= ['10']; then zero results returned.

enter image description here

The above returns 0 results. EndDates and SessionDetails data are OK for posts I'm testing. It seems that the correct SQL would be

( mt1.meta_key = 'EventTopicId' AND ( mt1.meta_value like '"10"' OR mt1.meta_value like '"11"' ... )

How would one craft a WP search query with multiple meta_keys, some strings, some arrays.

NOTE: I've tried this to cast as string with little results

array_walk($eventTopicId, function(&$value, $key) {
   return $value = '"'.$value.'"';
   //return $value = (string) $value;
});

UPDATE: This raw SQL works.

enter image description here

So the question still remains: How can you craft an SQL like this using WPQuery and MetaData.

1 Answer 1

1

The result was indeed another nested array using this answer as inspiration: https://wordpress.stackexchange.com/questions/104060/using-or-conditions-in-meta-query-for-query-posts-argument

'meta_query' =>
        array(
           'relation' => 'AND', // default relation
            array(
                'key' => 'met_key_1',
                'value' => '1128',
                'compare' => 'LIKE',
            ),
            array(
                'relation' => 'OR',
                array(
                    'key' => 'meta_key_2',
                    'value' => 'Friday',
                    'compare' => 'LIKE',
                ),
                array(
                    'key' => 'meta_key_2',
                    'value' => 'Saturday',
                    'compare' => 'LIKE',
                ),
            )
        )

So the resultant SQL was indeed AND between meta fields and OR within a specific SERIALISED dataset within a single meta field.

I looped over the IDs passed and created an $orSearch list enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.