最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - WordPress loop by meta key that is an array? and how loop multiple arrays

programmeradmin1浏览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

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

Share Improve this question asked Jun 21, 2019 at 1:24 Muhamed AhmedMuhamed Ahmed 157 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论