Support

Account

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

Unread

No results when querying REST API filtered by 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',
            array(
              'key'     => 'venue_city',
              'value'   => json_decode($_GET['chapter']),
              'compare' => '=',
            ),
            array(
              '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:

    https://www.canadalearningcode.ca/wp-json/wp/v2/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: https://www.canadalearningcode.ca/wp-json/wp/v2/experiences?venue_city=Toronto

    This example is the one that is failing: https://www.canadalearningcode.ca/wp-json/wp/v2/experiences?venue_city=St. 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 https://www.canadalearningcode.ca/wp-json/wp/v2/experiences?chapter_name=St 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 looked at similar problems on the forum but all of them are based on php queries and i can’t seem to find any solutions for a REST API query with apostrophes.

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

    Thanks so much!

    Jess

Viewing 1 post (of 1 total)

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