I’m creating a shortcode in wordpress where the user can use the post title or slug of a post to pull information from a post. Everything works well except for when a post’s title has an apostrophe in it.

An example of the shortcode that works with the post slug is


When I use the post title and the title has a apostrophe it doesn’t work.

[card]Hunter's Mark[/card]

When I use the shortcode with the title of a post that doesn’t contain a apostrophe everyting works so the issue is with the apostrophe. The code I use to get the post id is

$sql="select * from $wpdb->posts where (post_title='$content' or post_name='$content' ) and post_type='cards' and post_status='publish' limit 0,1";
$my_posts = $wpdb->get_results($sql);
if( $my_posts ) {
    $card_id = $my_posts[0]->ID;

Read more here: How to SQL query by post title in wordpress when title has an apostrophe


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , ,

Wordpress related questions and answers: