theme development - post_exists returning 0 if title contain special characters

admin2025-04-20  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.

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.

Share Improve this question edited Oct 2, 2019 at 14:05 Top-Bot 19910 bronze badges asked Oct 2, 2019 at 12:56 WaleedWaleed 213 bronze badges 4
  • What characters specifically are you using in your post titles? – Top-Bot Commented Oct 2, 2019 at 14:56
  • Mostly characters are – : * , . & – Waleed Commented 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 Commented Oct 4, 2019 at 13:18
  • Activities – More Than Games – Waleed Commented Oct 4, 2019 at 13:20
Add a comment  | 

1 Answer 1

Reset to default 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!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745106603a285327.html

最新回复(0)