最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - meta_query works locally but not on live server

matteradmin8PV0评论

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

Post a comment

comment list (0)

  1. No comments so far