php – get_page_by_title() doesn’t return the post object if the title has inverted comma (‘)

Question

The question is self-explanatory. I have tried these so far.

I have a post named John's Test with post id 1.

In single.php, I tried,

$extraPostID = 1;
$extraContent = get_page_by_title(get_the_title($extraPostID), OBJECT, 'post');
var_dump($extraContent);

Result: NULL

But this works,

$extraPostID = 1;
$extraContent = get_page_by_title('John\'s Test', OBJECT, 'post');
var_dump($extraContent);

Returns the post object as intended.

So, I thought fine, let’s add a slash to the title,

$extraPostID = 1;
$extraTitle = addslashes(get_the_title($extraPostID));
$extraContent = get_page_by_title($extraTitle, OBJECT, 'post');
var_dump($extraContent);

Returns NULL.

How about this

$extraPostID = 1
$str = html_entity_decode( get_the_title($extraPostID) );
$extraContent = get_page_by_title($str, OBJECT, 'post');
var_dump($extraContent);

Returns NULL.

So I am out of options. How do I make it to work?

0
Sahriar Saikat 2 months 2022-08-09T17:00:32-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse