Home Forums General Issues No results when querying REST API using ACF value that has apostrophe


No results when querying REST API using ACF value that has apostrophe

  • I’m trying to solve an issue on a website that uses Advanced Custom Fields PRO v5.8.7 and the ACF to REST API Plugin v3.2.0. WordPress version is 5.3.2.

    This is the code in my functions.php file that enables ACF field queries:

    function standard_acf_rest_query ($args) {
        $args['meta_query'][] = array();
        $ignore = array('per_page', 'page', 'search', 'order', 'orderby', 'slug', 'exclude', 'lang', 'offset', 'chapter');
        foreach ( $_GET as $key => $value ) {
          if (!in_array($key, $ignore)) {
            $args['meta_query'][] = array(
              'key'   => $key,
              'value' => $value,
        if (in_array('chapter', array_keys($_GET))) {
          $args['meta_query'][] = array(
            'relation' => 'OR',
              'key'     => 'venue_city',
              'value'   => json_decode($_GET['chapter']),
              'compare' => '=',
              'key'     => 'chapter_name',
              'value'   => json_decode($_GET['chapter']),
              'compare' => '='
        return $args;
      add_filter('rest_experiences_query', function( $args ) {
        $args["orderby"] = "meta_value";
        $args["meta_key"] = "start_date_and_time";
        return standard_acf_rest_query($args);

    I have a custom post type named experiences with an ACF text field named “venue_city“. I’m trying to query experiences filtered by the venue_city field. My queries are working fine except for the one city I have that has an apostrophe in the name. That being “St. John’s. When I query using a different ACF field called “chapter_name” which uses the value “St Johns Chapter”, that experience does come up as it should.

    Here are my queries:

    First, this general query will show you all my experiences:

    Now i want to filter by my ACF field named venue_city:

    Let’s start with a query that does bring back results just to show what it’s supposed to come back with:

    This example is the one that is failing: John’s

    (To see the experiences that have that venue_city value you can also filter by a different field called chapter_name and it will work because the chapter name value doesn’t have an apostrophe. Take a look at Johns Chapter

    I have tried doing the request with St.%20John%27s, St.+John%27s, St.+Johns, St.+John\s,St.+John%E2%80%99s

    In my php function enabling ACF queries I am using
    json_decode for the values of that field and I’ve also tried not using it as well as using esc_sql() and it still doesn’t work.

    I’ve read the other posts on the forum dealing with apostrophe querying issues but they’re all based on PHP queries.

    If you have any thoughts on how I might get that query to work, I would greatly appreciate your support.

    Thanks so much!


  • This is just a guess, but the ‘ is probably being escaped when sent \’ try 'value' => stripslashes($value), to see if that works

  • Thank you so much for your suggestion @hube21! I hadn’t tried that. Unfortunately it still doesn’t work.

    I thought it might after you suggested it because when I look at the database the value is saved as “St. John\’s”. No luck.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘No results when querying REST API using ACF value that has apostrophe’ is closed to new replies.