admin管理员组文章数量:1130349
I'm filtering posts by author's last name initials. On my local server, the query runs beautifully, but when I push live, it doesn't. It doesn't find anything. Does it have anything to do with the way I'm escaping the custom field value?
$author = get_query_var('author-initials');
if (!empty($author)) {
$initials = explode('-', $author);
$value = array();
foreach($initials as $initial) {
$value[] = strtolower($initial);
$value[] = strtoupper($initial);
}
$meta_query[] = array(
'key' => 'whitepaper_author',
'value' => "\s[" . implode('', $value) . "]\w+$",
'compare' => 'REGEXP'
);
}
if (count($meta_query) > 1) {
$meta_query['relation'] = 'OR';
}
$query->set('meta_query', $meta_query);
I'm filtering posts by author's last name initials. On my local server, the query runs beautifully, but when I push live, it doesn't. It doesn't find anything. Does it have anything to do with the way I'm escaping the custom field value?
$author = get_query_var('author-initials');
if (!empty($author)) {
$initials = explode('-', $author);
$value = array();
foreach($initials as $initial) {
$value[] = strtolower($initial);
$value[] = strtoupper($initial);
}
$meta_query[] = array(
'key' => 'whitepaper_author',
'value' => "\s[" . implode('', $value) . "]\w+$",
'compare' => 'REGEXP'
);
}
if (count($meta_query) > 1) {
$meta_query['relation'] = 'OR';
}
$query->set('meta_query', $meta_query);
Share
Improve this question
edited Oct 20, 2018 at 21:40
Gabriel H.
asked Oct 20, 2018 at 21:00
Gabriel H.Gabriel H.
334 bronze badges
15
|
Show 10 more comments
1 Answer
Reset to default 0Please try using (blank space) instead of \s and . instead of \w
For further details on supported operators and characters please check this link: https://dev.mysql/doc/refman/5.7/en/regexp.html
I'm filtering posts by author's last name initials. On my local server, the query runs beautifully, but when I push live, it doesn't. It doesn't find anything. Does it have anything to do with the way I'm escaping the custom field value?
$author = get_query_var('author-initials');
if (!empty($author)) {
$initials = explode('-', $author);
$value = array();
foreach($initials as $initial) {
$value[] = strtolower($initial);
$value[] = strtoupper($initial);
}
$meta_query[] = array(
'key' => 'whitepaper_author',
'value' => "\s[" . implode('', $value) . "]\w+$",
'compare' => 'REGEXP'
);
}
if (count($meta_query) > 1) {
$meta_query['relation'] = 'OR';
}
$query->set('meta_query', $meta_query);
I'm filtering posts by author's last name initials. On my local server, the query runs beautifully, but when I push live, it doesn't. It doesn't find anything. Does it have anything to do with the way I'm escaping the custom field value?
$author = get_query_var('author-initials');
if (!empty($author)) {
$initials = explode('-', $author);
$value = array();
foreach($initials as $initial) {
$value[] = strtolower($initial);
$value[] = strtoupper($initial);
}
$meta_query[] = array(
'key' => 'whitepaper_author',
'value' => "\s[" . implode('', $value) . "]\w+$",
'compare' => 'REGEXP'
);
}
if (count($meta_query) > 1) {
$meta_query['relation'] = 'OR';
}
$query->set('meta_query', $meta_query);
Share
Improve this question
edited Oct 20, 2018 at 21:40
Gabriel H.
asked Oct 20, 2018 at 21:00
Gabriel H.Gabriel H.
334 bronze badges
15
-
I'm curious, but where is this code snippet? Where does the
$queryvariable come from? Is this apre_get_postsfilter? Note that trying to filter/find posts by their post meta is extremely slow, and can bring powerful servers to their knees quickly. That you're trying to do a regular expression is even worse! Are you sure you wouldn't prefer a custom taxonomy namedwhitepaper_author? It could be as much as 10,000x faster and more scalable, running it on your local server is deceptive as you are the only person visiting that site and it has a full CPU to run the query – Tom J Nowell ♦ Commented Oct 20, 2018 at 21:09 - Maybe a stupid comment, but have you made sure the DB is exactly the same locally and live? Maybe there's just nothing to find. – Pim Commented Oct 20, 2018 at 21:15
- The $query comes from an pre_get_posts action hook. I guess I'm not having problems with speed, but thanks for the advice. I'm just not getting the results I want from that meta_query array on the live server. I'm also running a tax_query array inside the same $query, and it finds posts based on the taxonomies I select. – Gabriel H. Commented Oct 20, 2018 at 21:23
- @Pim Yeah, the DB is exactly the same. Does the problem have anything to do with the way I wrote those escaping sentences? – Gabriel H. Commented Oct 20, 2018 at 21:29
- have you checked the collation? – middlelady Commented Oct 20, 2018 at 21:41
1 Answer
Reset to default 0Please try using (blank space) instead of \s and . instead of \w
For further details on supported operators and characters please check this link: https://dev.mysql/doc/refman/5.7/en/regexp.html
本文标签: wp querymetaquery works locally but not on live server
版权声明:本文标题:wp query - meta_query works locally but not on live server 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749184734a2329196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$queryvariable come from? Is this apre_get_postsfilter? Note that trying to filter/find posts by their post meta is extremely slow, and can bring powerful servers to their knees quickly. That you're trying to do a regular expression is even worse! Are you sure you wouldn't prefer a custom taxonomy namedwhitepaper_author? It could be as much as 10,000x faster and more scalable, running it on your local server is deceptive as you are the only person visiting that site and it has a full CPU to run the query – Tom J Nowell ♦ Commented Oct 20, 2018 at 21:09