Escaped how? How to escape an apostrophe in a filter expression

I’m using the Admin API and have a post title with an apostrophe in it. I need to search the Admin API to find it. The admin api docs refer to the content api docs, which say:

Single or Double quote __MUST __be escaped*

See that asterisk? It doesn’t go anywhere. I’ve tried url encoding my ', which doesn’t break anything, but doesn’t match the post title either. I’ve tried not encoding, but that breaks everything, because it messes up the filter syntax.

I could use a hint.

Frustratedly yours…

And I should be a better poster and provide more details. Sample code:

// attempt 1 
      let title = `What's Happening in Golden - ${date}`
      let titleFilter = `title:"${title}"`;
      let postExists = await api.posts.browse({filter: titleFilter});
// ^^ throws an error: "Request not understood error, cannot list posts." "context":"Error parsing filter"

// attempt 2
      let title = `What%27s Happening in Golden - ${date}`
      let titleFilter = `title:"${title}"`;
      let postExists = await api.posts.browse({filter: titleFilter});
// ^^ Same error

Based on the docs, you need to enclose your filter with single quotes. Here’s what worked for me:

title:~^'What\\'s'
              ^ This is how you escape the next character.
                The actual filter ends up with one backslash,
                  but because we're using JS, we use 2 since JS also
                  uses a backslash for escaping

Translation: “The title must start with What’s”
Filter in the URL: filter=title%3A~%5E%27What%5C%27s%27
Filter in JavaScript (definitely don’t use single quotes for the JS string :grinning:):
const filter = "title:~^'What\\'s'";

1 Like

THANK YOU! Going to try it out!