php - 当要比较的元数据是序列化数组时,Wordpress meta_query 值数组?

我正在尝试运行一个带有值数组的 meta_query 并让它搜索是否所有内容都存在于存储在序列化数组中的元值中。这可能吗?

我的查询参数如下(注意这是嵌套在一个类中):

$args = array(
    'post_type' => $this->posttype,
    'posts_per_page' => '9',
    'paged' => $paged,
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'meta_key' => 'lumens',
    'meta_query' => array(
        array(
            'key' => 'mount',
            'value' => array('pendant' , 'wall'),
            'compare' => 'IN'
        )

    )
);

存储元数据的一个例子,是在类似于下面的序列化数组中:

a:4:{i:0;s:7:"pendant";i:1;s:15:"surface-ceiling";i:2;s:4:"wall";i:3;s:14:"aircraft-cable";}

无论我尝试什么,我的查询都不会返回适当的结果。我现在意识到我可能应该将每个值存储在不同的元键中而不是数组中,但是,现在已经有很多条目可以更改元数据。

更新:

这是我的解决方法,类似于@Leander 方法;由于数据库中已有的条目数量,我不想更改序列化输入,还有一件事我忘了提,我正在使用 CMB2 Developer Toolkit它将复选框字段存储为 native 序列化数据。

// This is pulled from a user input
$meta_queries = array('pendent' , 'wall');

// Metaqueries are passed through loop to create single entries with the like comparison operator
foreach($meta_queries as $key => $value){

    $meta_query = array(
        'key' => '_tf_' . $key,

        // Serialize the comparison value to be more exact
        'value' => serialize(strval($value)),
        'compare' => 'LIKE',
    );

}

// Generate $args array
$args = array(

    'post_type' => $this->posttype,
    'posts_per_page' => '9',
    'paged' => $paged,
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'meta_key' => 'lumens',
    'meta_query' => $meta_queries

);

在填充数据时,我没有注意到太多性能问题。我想如果要处理的数据量巨大,就必须重新设计这种方法。

最佳答案

刚遇到同样的问题...!

我有一些标签存储在一个数组/自定义字段中,我正在使用搜索表单查询我的帖子,用户应该被允许搜索多个标签,所以基本上我需要将一个数组与另一个数组进行比较。

I am realizing now that I probably should have stored each value in a different meta-key rather then in an array, however, there is far to many entries already to change the metadata now.

我想这会产生相当大的开销,不建议...

方法/解决方法

我将用户输入作为字符串处理,从字符串中创建一个数组来检查它的大小,并根据大小创建单个 LIKE 比较,它可以很好地处理我的数组数据。

$tags_string = get_query_var( 'p_tags' ); // form submitted data
$tags_array = explode( ',', $tags_string ); // create array

if ( count( $tags_array ) > 1 ) { // check if more then one tag
$meta_query['p_tags']['relation'] = 'AND';

foreach($tags_array as $tag) { // create a LIKE-comparison for every single tag
    $meta_query['p_tags'][] = array( 'key' => 'YOUR_KEY', 'value' => $tag, 'compare' => 'LIKE' );
}
} else { // if only one tag then proceed with simple query
    $meta_query['p_tags'] = array( 'key' => 'YOUR_KEY', 'value' => $tags_string, 'compare' => 'LIKE' );
}

Args 输出(演示)

[meta_query] => Array
        (
            [p_tags] => Array
                (
                    [relation] => AND
                    [0] => Array
                        (
                            [key] => basic_tags
                            [value] => adobe
                            [compare] => LIKE
                        )

                    [1] => Array
                        (
                            [key] => basic_tags
                            [value] => stone
                            [compare] => LIKE
                        )

                )

        )

注意:根据数组的大小、要查询的帖子数量等,此解决方案可能不是性能最高的可用解决方案。

另一种方法可能是 WordPress 查询的 FIND_IN_SET 扩展,see this gist .

感谢任何有关性能或提高代码质量的意见。

https://stackoverflow.com/questions/43789776/

相关文章:

php - WooCommerce 以编程方式获取购物车 ID

qt - 对 `dlsym' 的 undefined reference

r - 观星者,无输出

visual-studio-code - VS code终端光标效果

django - 值错误 : Cannot use Queryset for "": Use a Q

ruby-on-rails - 使用 Chartkick 和 Rails 的多系列折线图

python - 如何使用 Python Selenium 获取多个元素的文本内容?

php - Laravel contains() 用于多个值

json - JSON 模式中的变量对象名称

angular - 如何正确检查用户是否在 Angular4 中经过身份验证?