admin管理员组文章数量:1130349
I have to use the meta_query argument so that it checks for a keyword in all available custom field values (in array format) saved for the object. The meta keys are different for every object, therefor I can't use any static key.
This is something I want to achieve.
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'state', // This is where I am lost
'value' => 'mykeyword', //Keyword to look for
'compare' => 'EXISTS' //If keword exists in value (array)
),
),
Can this be done using meta_query or do I need to use custom database query?
Thanks
I have to use the meta_query argument so that it checks for a keyword in all available custom field values (in array format) saved for the object. The meta keys are different for every object, therefor I can't use any static key.
This is something I want to achieve.
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'state', // This is where I am lost
'value' => 'mykeyword', //Keyword to look for
'compare' => 'EXISTS' //If keword exists in value (array)
),
),
Can this be done using meta_query or do I need to use custom database query?
Thanks
- I’m afraid that the only solution is to use custom SQL with LIKE using posts_where and posts_join filters – Krzysiek Dróżdż Commented Jan 10, 2019 at 10:49
1 Answer
Reset to default 0Short of coming up with a MySQL query yourself, you could get a list of distinct meta_keys and build your query array based on the results.
On another note, I think you may have misunderstood the meaning of EXISTS. It doesn't denote finding the value parameter passed within the value of the given meta. I think you actually mean LIKE, but please correct me if I'm wrong.
$query_arg = array(
...
);
global $wpdb;
$meta_keys = $wpdb->get_col("SELECT DISTINCT `meta_key` FROM {$wpdb->postmeta}");
foreach($meta_keys as $meta_key){
$query_arg['meta_query'][] = array(
'key' => $meta_key,
'value' => 'mykeyword',
'compare' => 'LIKE'
);
}
$query_arg['relation'] = 'OR';
Although this is likely not the most optimised way of doing things, from a MySQL standpoint.
I have to use the meta_query argument so that it checks for a keyword in all available custom field values (in array format) saved for the object. The meta keys are different for every object, therefor I can't use any static key.
This is something I want to achieve.
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'state', // This is where I am lost
'value' => 'mykeyword', //Keyword to look for
'compare' => 'EXISTS' //If keword exists in value (array)
),
),
Can this be done using meta_query or do I need to use custom database query?
Thanks
I have to use the meta_query argument so that it checks for a keyword in all available custom field values (in array format) saved for the object. The meta keys are different for every object, therefor I can't use any static key.
This is something I want to achieve.
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'state', // This is where I am lost
'value' => 'mykeyword', //Keyword to look for
'compare' => 'EXISTS' //If keword exists in value (array)
),
),
Can this be done using meta_query or do I need to use custom database query?
Thanks
- I’m afraid that the only solution is to use custom SQL with LIKE using posts_where and posts_join filters – Krzysiek Dróżdż Commented Jan 10, 2019 at 10:49
1 Answer
Reset to default 0Short of coming up with a MySQL query yourself, you could get a list of distinct meta_keys and build your query array based on the results.
On another note, I think you may have misunderstood the meaning of EXISTS. It doesn't denote finding the value parameter passed within the value of the given meta. I think you actually mean LIKE, but please correct me if I'm wrong.
$query_arg = array(
...
);
global $wpdb;
$meta_keys = $wpdb->get_col("SELECT DISTINCT `meta_key` FROM {$wpdb->postmeta}");
foreach($meta_keys as $meta_key){
$query_arg['meta_query'][] = array(
'key' => $meta_key,
'value' => 'mykeyword',
'compare' => 'LIKE'
);
}
$query_arg['relation'] = 'OR';
Although this is likely not the most optimised way of doing things, from a MySQL standpoint.
本文标签: meta querymetaquery to check all custom fields
版权声明:本文标题:meta query - meta_query to check all custom fields 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749025765a2305079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论