Support » Fixing WordPress » 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://wordpress.org/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

    • This topic was modified 1 year, 4 months ago by nicole2292.
    • This topic was modified 1 year, 4 months ago by nicole2292.
    • This topic was modified 1 year, 4 months ago by nicole2292.
    • This topic was modified 1 year, 4 months ago by nicole2292.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi Nicole,

    May I ask, why do you need to use the get_page_by_title() function? I mean, what will you do with the value returned by that function?

    I’m wondering if there would be an alternative solution for your use case.

    Thanks in advance for your reply.

    Regards,
    Paulo

    Thread Starter nicole2292

    (@nicole2292)

    Hi Paulo,

    The reason I need to use get_page_by_title() is that this is a highly complex site and there is a need for two different views of essentially the same post content on two different URL’s. This need was covered in an earlier topic I started here: https://wordpress.org/support/topic/multiple-views-of-a-single-post/

    So we have one master post in a custom post type (lets call it A) which has the main post content and all the custom fields and custom meta data required for that post. It is a complex post with a lot of data.

    Then we have a duplicate of that post created via export and import of just the title in a different custom post type (lets call it B). This has the same post title. Then in the single template to display this post we are pulling most of the content from the master post of the same title in post type A using the get_page_by_title() function.

    The end result is two quite different single post views like this:
    ourdomain.com/posttype-A/our-post-title
    ourdomain.com/posttype-B/our-post-title

    I cannot think of another better way to do this. I really just need get_page_by_title() to actually work 🙂

    Hi Nicole,

    Thank you for the detailed information, I now have a better understanding of what you intend to achieve.

    However, I’m confused by something you said in your original post above:

    That isn’t going to work as the path (post slug) isn’t always the same as the sanitized title.

    Does this mean that the slug of posttype-B will change so that you have something like the following?

    ourdomain.com/posttype-A/our-post-title
    ourdomain.com/posttype-B/our-post-title-changed

    If yes, wouldn’t this mean that the post title also changed, and so that you wouldn’t be able to to use get_page_by_title() either? I must admin I’m a bit confused.

    ===

    I think, ultimately, what you want is to establish a relationship between a post and their “parent”. In your example above, posttype-B/our-post-title is a “child” of posttype-A/our-post-title.

    Currently, you’re establishing this relationship implicitly through the title of the post, which is giving you problems due to the behavior of the get_page_by_title() function. Another implicit way to establish the relationship, would be through the slug, but this might not be an option if the slug can change (as mentioned above).

    Alternatively, you could establish the relationship explicitly, by requiring that a “child” page has a parent. There are multiple ways to do this, with the simpler one probably being to use the “parent page” attribute: https://wordpress.com/support/pages/page-options/#parent-page.

    For each “child” page, you would explicitly select the “master” page as their parent. You would then, instead of using get_page_by_title() to retrieve the parent page, just use the parent attribute that has been explicitly selected for the current page.

    I hope this helps you. I realize this is not an answer to this thread’s topic (get_page_by_title() doesn’t work if title has an apostrophe), but I think it could be a solid solution to your problem.

    Regards,
    Paulo

    Thread Starter nicole2292

    (@nicole2292)

    Hi Paulo,

    Thanks for your lengthy reply.

    Yes your understanding that the slug might change is correct. However the post title and the slug are independant of each other so the post titles could remain the same even if the slug format was changed.
    get_page_by_title() should work as long as the post titles are the same regardless of the slug.

    One post is not a child of the other as they are in two different post types. They are not two posts in the same post type which is heirarchical and thus supports parent and child relationships. It would not work to set them up this way as then they would all be in the one post type which does not create the desired functionality.

    Ultimately I’m not looking for a workaround for get_page_by_title(). I know how to achieve that… I could set up a custom field on the B post to enter the post ID of the master A post and create a relationship that way. However that is extra work for each and every post and it should not be necessary to do that at all given there is a function get_page_by_title() which theorhetically does exactly what we need more efficiently… unless there are special characters!

    So how can we just make get_page_by_title() actually work as it should rather than looking for hacky workarounds?

    Hi Nicole, sorry for being late to the party.

    I’m not sure if you have resolved your issue or not yet.

    Have you tried using the PHP function html_entity_decode on the title before passing it into the get_page_by_title function?

    Something like this:

    $decoded_title = html_entity_decode( $post_title );
    $post = get_page_by_title( $decoded_title, OBJECT, 'post' );
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get_page_by_title() doesnt work if title has an apostrophe ( ‘ )’ is closed to new replies.