wp query - meta_query works locally but not on live server

admin2025-06-06  9

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 $query variable come from? Is this a pre_get_posts filter? 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 named whitepaper_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
 |  Show 10 more comments

1 Answer 1

Reset to default 0

Please 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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749184761a317071.html

最新回复(0)