1

I want to write a query which fetches the data based on my meta key added. Meta key name is 'repeater'.

This is my POST META.

Serialized:

a:3:{i:0;a:3:{s:9:"postdates";s:10:"2021-02-02";s:12:"postplatform";a:1:{i:0;s:3:"3DS";}s:10:"postregion";a:2:{i:0;s:2:"EU";i:1;s:2:"JP";}}i:1;a:3:{s:9:"postdates";s:10:"2021-02-03";s:12:"postplatform";a:2:{i:0;s:3:"3DS";i:1;s:2:"DS";}s:10:"postregion";a:2:{i:0;s:2:"EU";i:1;s:2:"JP";}}i:2;a:3:{s:9:"postdates";s:10:"2021-02-04";s:12:"postplatform";a:3:{i:0;s:3:"3DS";i:1;s:2:"DS";i:2;s:27:"MAC, Linux, Mobile And More";}s:10:"postregion";a:3:{i:0;s:2:"EU";i:1;s:2:"JP";i:2;s:2:"NA";}}}

Unserialized:

Array
(
    [0] => Array
        (
            [postdates] => 2021-02-02
            [postplatform] => Array
                (
                    [0] => 3DS
                )

            [postregion] => Array
                (
                    [0] => EU
                    [1] => JP
                )

        )

    [1] => Array
        (
            [postdates] => 2021-02-03
            [postplatform] => Array
                (
                    [0] => 3DS
                    [1] => DS
                )

            [postregion] => Array
                (
                    [0] => EU
                    [1] => JP
                )

        )

    [2] => Array
        (
            [postdates] => 2021-02-04
            [postplatform] => Array
                (
                    [0] => 3DS
                    [1] => DS
                    [2] => MAC, Linux, Mobile And More
                )

            [postregion] => Array
                (
                    [0] => EU
                    [1] => JP
                    [2] => NA
                )

        )

)

Now, I want to fetch the POST if postdates is today/ specific dates.

Thanks, Ali

7
  • 2
    You can't query serialized data that way. You will need to structure your data differently. – Jacob Peattie Feb 2 at 12:46
  • 1
    Does this answer your question? meta_query with meta values as serialize arrays – cameronjonesweb Feb 2 at 12:47
  • @JacobPeattie Can you please let me know how can I structure that data ? – aliasgar vanak Feb 2 at 12:50
  • I don't know enough about your data to know what's appropriate, but any meta value that you want to query by needs its own key. It looks like the dates should be their own key, and the regions and platforms should be taxonomies. – Jacob Peattie Feb 2 at 13:30
  • 1
    You can't store the data in a single post meta, it has to be multiple separate values for each piece of data. Are you using an ACF repeater field? You can't query sub-values of serialized data – Tom J Nowell Feb 2 at 13:35
0

This is not possible, you cannot query sub-values of serialised/structured data in post meta values.

To fix this, you need to store the dates you want to query/filter by as separate post meta key/value pairs. Values need to be string/number/boolean whole values to be queried this way. They cannot be complex structures or sub-strings, such as JSON, arrays, objects

Also note that saving an array or object is a security risk. This is because you can't save an array/object so WordPress uses PHP's serialize function, turning it into a string. This exposes you to object deserialisation attacks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.