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

Der*_*rez 2 php wordpress metadata cmb2

我正在尝试使用一组值运行 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'
        )

    )
);
Run Code Online (Sandbox Code Playgroud)

存储的元数据的示例位于类似于以下的序列化数组中:

a:4:{i:0;s:7:"pendant";i:1;s:15:"surface-ceiling";i:2;s:4:"wall";i:3;s:14:"aircraft-cable";}
Run Code Online (Sandbox Code Playgroud)

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

更新:

这是我的解决方法,类似于@Leander 方法;由于数据库中已有的条目数量,我不想更改序列化输入,我忘记提及一件事,我正在使用CMB2 开发人员工具包,它将复选框字段存储为本地序列化数据。

// 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

);
Run Code Online (Sandbox Code Playgroud)

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

Lea*_*der 7

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

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

我现在意识到我可能应该将每个值存储在不同的元键中,而不是存储在数组中,但是,现在已经有很多条目可以更改元数据。

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

方法/解决方法

我将用户输入作为字符串处理,从字符串中创建一个数组以检查其大小,并根据大小创建单个 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' );
}
Run Code Online (Sandbox Code Playgroud)

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
                        )

                )

        )
Run Code Online (Sandbox Code Playgroud)

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

另一种方法可能是 WordPress 查询的 FIND_IN_SET 扩展,请参阅此要点

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