get_page_by_title() doesnt work if title has an apostrophe ( ‘ )
-
Hello,
This seems to be a common problem from my research however I have as yet been unable to find a solution.
I am simply trying to use get_page_by_title() and it normally works unless there are special characters in the title. & breaks it. However I told the content editors just to use “and” or
& a m p;
(without the space) instead as a workaround.Now we have discovered that having an apostrophe in the title also breaks the get_page_by_title() function. We can’t just not use an apostrophe and I can’t tell the content editors to escape every apostrophe using
& # 0 3 9 ;
(without spaces) as there are already many posts on the site with apostrophes in them. It’s too common to use such a workaround.The get_page_by_title() function really should work with apostrophes.
Example Post title: “Nicole’s Test Post”
get_page_by_title("Nicole's Test Post", $output = OBJECT, $post_type = "post");
Nothing is returned. However if I remove the apostrophe it works as expected.
Now I have read many other support requests around this issue such as this one: https://www.ads-software.com/support/topic/get_page_by_title-doesnt-work-if-title-has-an-apostrophe/
Also I have seen a suggested workaround using sanitize_title() and get_page_by_path(). Eg. https://wordpress.stackexchange.com/questions/287347/get-page-by-title-with-an-apostrophe-in-variable/287352 That isn’t going to work as the path (post slug) isn’t always the same as the sanitized title. I know they are set the same by default when a post is first created however they are often changed after that so then they are different. We certainly cannot assume that they are the same.
So how can I make the get_page_by_title() function work for posts which have an apostrophe in them?
We are using the latest version of WordPress which at the time of writing is 5.7.1.
Please note that if it is relevant our site is set to define( ‘DB_CHARSET’, ‘utf8mb4_unicode_520_ci’ );
This setting is required or the Google Maps API does not work for addresses with foreign characters in them. Our site is international so we have maps for locations with foreign characters in them. The CHARSET cannot be changed. Nor should it need to be for a core wordpress function to work ??
Can anyone help?
Thanks very much,
Nicole
- The topic ‘get_page_by_title() doesnt work if title has an apostrophe ( ‘ )’ is closed to new replies.