Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#46355 closed defect (bug) (worksforme)

get_page_by_title() doesnt work if title has an apostrophe ( ‘ )

Reported by: wpnerdzz's profile WPnerdzz Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.1
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

Here is the code in my theme.

$lz_version= get_post_meta( $post->ID, 'option_current-release', true );

a metabox with a number like 1.9. Custom Post type: Books

$lz_name = get_the_title().' - '.$lz_version;

it echos something like "Alex’s book collection – 1.9"

$lz_links = get_page_by_title($lz_name, OBJECT, 'release_link');

release_link is a custom post type. The titles of that CPT are like “Alex’s book collection – 1.9”

echo get_the_title($lz_links>ID);

it echos the title of current page “Alex’s book collection”, not desired “Alex’s book collection – 1.9”
But if I dont add any apostrophe, the code works fine.

Change History (3)

#1 follow-up: @immeet94
6 years ago

Hi, @WPnerdzz thanks for your ticket and welcome to WordPress Trac :)
you try to use sanitize_title() function for the getting title.

here the example for:
$content = sanitize_title( $content );

#2 in reply to: ↑ 1 @WPnerdzz
6 years ago

Replying to immeet94:

Hi, @WPnerdzz thanks for your ticket and welcome to WordPress Trac :)
you try to use sanitize_title() function for the getting title.

here the example for:
$content = sanitize_title( $content );

How can I use sanitize_title() inside get_page_by_title()?
sanitize_title() outputs something like alexs-book-collection which is not the title so the get_page_by_title() function doesnt work.
I can make a workaround like this,

url_to_postid( get_site_url().'/release_link/'.sanitize_title( get_the_title() ).' - '.$lz_version );

but it has a major flaw because if I change the title of a post, the url doesnt change automatically (which is a good thing for other purposes) so it wont work. And this site has hundred of entries everyday so editing both title and permalink is very inefficient.
And the actual bug we are talking about is, if a title has apostrophe, get_page_by_title() wont work. I am looking for a fix for this.

#3 @desrosj
6 years ago

  • Focuses coding-standards removed
  • Keywords needs-patch removed
  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed
  • Version changed from 5.1 to 2.1

Hi @WPnerdzz,

Thanks for this ticket and welcome to Trac!

I did some testing on this and I was unable to reproduce the behavior you are describing. When I create a page with an apostrophe in the title, I am correctly able to retrieve the page with get_page_by_title().

While investigating, I noticed that get_page_by_title() does not have any unit tests. I opened #46439 to add these. When I wrote the tests I and made sure to include assertions that proves the function works as expected when there apostrophes and quotation marks in the title.

In your examples above, the character in Alex’s is actually a right single quotation mark (U+2019) and not an apostrophe (U+0027). I think that your code may be looking for an apostrophe, but the code is actually inserting a different character.

I am going to close this as worksforme because the tests show the function is working correctly and I am unable to reproduce the issue. If after you verify that your characters are in fact the same you are still experiencing this issue, you can reopen this and report back with more details.

Note: See TracTickets for help on using tickets.