Works for me on my site. Your code, but different title. There was an issue about apostrophes in titles a while back. Something about titles being double escaped. But it has been fixed now. Are you using the latest WP version? If not, you should update.
If you are on the latest, try deactivating all plugins. Your code should then work. Then reactivate plugins one by one. When your code again fails, the last activated plugin is the problem.
If your code fails with no plugins, the problem could be your theme. Switch to a twenty* default theme and temporarily place your test code on a theme template. It will work with a default theme and no plugins.
If the post title is “Alex’s book collection – 1.9”, what is the point of $lz_name = get_the_title().' - '.$lz_version;
? That would result in “Alex’s book collection – 1.9 – 1.9″ and getting a page by that title would fail.
If the post title is “Alex’s book collection”, and you did $lz_name = get_the_title().' - '.$lz_version;
, that would result in “Alex’s book collection – 1.9″ and getting a page by that title would fail because the title is “Alex’s book collection”. Or is there one post of each title?
In any case, in my experience, get_page_by_title() returns the post requested if it exists, apostrophe or not. If this is not happening for you, it is likely due to interference from your theme or a plugin.
No no no…
The title in Post type: book is “Alex’s book collection”
In single book page, I am trying to post info from another post from different post type: release_link named “Alex’s book collection – 1.9”.
The title of this post type has a format like <book name> – <version>. I added a meta box in post type: book where I can enter book version (1.9 in this case).
$lz_name = get_the_title().' - '.$lz_version;
$lz_links = get_page_by_title($lz_name, OBJECT, 'release_link');
This works only if there is no inverted commas in book title.
The post title where I am adding this code is “Alex’s book collection”.
The other post I am trying to get info from is “Alex’s book collection – 1.9”
I see now, thanks for explaining. I set up the same scenario on my site. Different custom post type names, but exact same titles. Your code returns the correct post on my site with an apostrophe in the title. I’m unable to replicate the problem, so I can’t investigate the root cause.
Please install and activate the health-check plugin. The health check item shows up in the left admin menu under Dashboard. Go to the health check screen, then enter troubleshooting mode. This mode will be using the twentynineteen theme, so temporarily place your code inside the Loop of the theme’s single.php template. In troubleshooting mode, view the “Alex’s book collection” post. Your code should now return the appropriate “release_link” post.
Use the admin bar troubleshooting item to restore your normal theme and plugins, one at a time, testing after each. When the wrong post comes back again, the last activated module is the one causing trouble.