0

How can I get loop of posts by meta values that save in a meta_key that is an array like that

$sharedHosting_plans_fields = Array
                                    (
                                        [0] => Array
                                            (
                                                [plan_name] => name 1
                                                [plan_price] => 1
                                                [theType] => yesPaid
                                                [plan_space] => Unlimited
                                            )

                                        [1] => Array
                                            (
                                                [plan_name] => name 2
                                                [plan_price] => 3
                                                [theType] => yesFree
                                                [plan_space] => 5000
                                            )

                                        [2] => Array
                                            (
                                                [plan_name] => name2
                                                [plan_price] => 5
                                                [theType] => yesPaid
                                                [plan_space] => Unlimited
                                            )

                                    )

And I have used this

     'meta_key' => 'sharedHosting_plans_fields',
     'orderby' => 'meta_value_num meta_value ',

but no effect!!!

2-If i want to get posts by checking multiple meta keys like that

    array(
         'key' => 'plan_type2'
    ),
    array(
         'key' => 'plan_type3'
    ),
    array(
         'key' => 'plan_type4'
    ),
    array(
         'key' => 'plan_type5'
    ),

How I can short hand this code by for loop or any thing else?

I have tried to use

    'meta_key' => array('sharedHosting_plans_fields'),
     'orderby' => 'meta_value_num meta_value ',

instead of

    'meta_key' => 'sharedHosting_plans_fields',
     'orderby' => 'meta_value_num meta_value ',

but no luck

1

You can't order by values inside serialised arrays in post meta. When you store an array like this:

$value = [
    [
            'plan_name'  => 'name 1',
            'plan_price' => '1',
            'theType'    => 'yesPaid',
            'plan_space' => 'Unlimited',
    ],
    [
            'plan_name'  => 'name 2',
            'plan_price' => '3',
            'theType'    => 'yesFree',
            'plan_space' => '5000',
    ],
    [
            'plan_name'  => 'name2',
            'plan_price' => '5',
            'theType'    => 'yesPaid',
            'plan_space' => 'Unlimited',
    ],
]

This is stored in PHP like this:

a:3:{i:0;a:4:{s:9:"plan_name";s:6:"name 1";s:10:"plan_price";s:1:"1";s:7:"theType";s:7:"yesPaid";s:10:"plan_space";s:9:"Unlimited";}i:1;a:4:{s:9:"plan_name";s:6:"name 2";s:10:"plan_price";s:1:"3";s:7:"theType";s:7:"yesFree";s:10:"plan_space";s:4:"5000";}i:2;a:4:{s:9:"plan_name";s:5:"name2";s:10:"plan_price";s:1:"5";s:7:"theType";s:7:"yesPaid";s:10:"plan_space";s:9:"Unlimited";}}

As far as MySQL — which is responsible for querying posts — is concerned, this is just a long string. Indistinguihsable from a word or sentence that begins with a. It's only turned back into an array by WordPress when the value is retrieved with get_post_meta().

So it's not possible to sort by a property within an array, and if you attempt to sort by the meta value of this meta then it's just going to sort as if this were a string.

If you need to sort this way then you need to find an alternate way of storing the data. I don't have enough insight into your project to suggest anything specific, but you probably need a custom database table for these plans, which you'd then need to join and sort using the posts_join and posts_orderby hooks to modify the query SQL directly.

  • Thanks but I want to get posts that have this meta value "yesPaid" and you have told me that's will happen if I sort this value in a separated field only so I ask How can I search inside this array meta key? 2-It's an array because it have many values from repeated custom fields 3-Could you answer my question number two? – meno Jun 21 '19 at 3:16
  • I answered your question, at length: You can’t. – Jacob Peattie Jun 21 '19 at 4:06

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.