0

I am using the below code:

$title = get_the_title($result->post_id);

if ( 0 === post_exists( $title ) ) {
$title = 'Document Id: '.$result->post_id .' (Deleted)';
}

But it's returning 0 if post title contains special characters. I also tried using

$title = esc_attr(get_the_title($result->post_id));

but no luck.

4
  • What characters specifically are you using in your post titles?
    – Top-Bot
    Oct 2, 2019 at 14:56
  • Mostly characters are – : * , . &
    – Waleed
    Oct 3, 2019 at 13:36
  • Try ``` $title = get_the_title($result->post_id); echo $title; ``` and post the result here so I can see how the data is being stored in the DB
    – Top-Bot
    Oct 4, 2019 at 13:18
  • Activities – More Than Games
    – Waleed
    Oct 4, 2019 at 13:20

1 Answer 1

2

Looks like WP is using HTML entity encoding on the special characters, you need to parse the title using the html_entity_decode() function like so.

$title = html_entity_decode(get_the_title($result->post_id));

if ( 0 === post_exists( $title ) ) {
$title = 'Document Id: '.$result->post_id .' (Deleted)';
}

I am not sure if the post exists function decodes the characters but I am going to assume it does, let me know if you are still getting a 0 though

Comment below any questions!

2
  • This doesn't work. It still returning 0.
    – Waleed
    Oct 4, 2019 at 17:46
  • 1
    Works for me, thank you.
    – rank
    Sep 2, 2020 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.