0

I'm using a simple $wpdb query that searches the database for all other posts with the same title as the current one and then displays a link to them on the current post.

So if I go to a post titled RED SHOES it will display at the top of the screen a link to all other posts with the title RED SHOES.

It works fine for all post titles except those with a single quote in them. Other special characters work fine.

A post with the title THE WEATHER: SUNNY will feature a list to all other posts with that title. But a post with the title TODAY'S WEATHER IS NICE will not find any other posts with that title even though they exist, because it contains a single quote.

I've searched for a solution but haven't found one. Can anyone suggest a way around this?

Here's the code I'm using:

global $wpdb;
$itemName = get_the_title( $ID );
$results =$wpdb->get_results("SELECT ID FROM $wpdb->posts where  $wpdb->posts.post_title LIKE '$itemName' AND $wpdb->posts.post_status = 'publish' ORDER BY post_title ASC", OBJECT);

foreach ($results as $result){
$newTitle =$wpdb->get_results("SELECT post_title FROM $wpdb->posts where  $wpdb->posts.ID = $result->ID", OBJECT);
printf ($newTitle->post_title);
}

By the way, I realise that code will only display the title of other pages and not the link. I've removed the 'link' section as it works fine, so it's not necessary here.

EDIT:

This is the updated code based on userabuser's suggestion. It's currently returning an error "Unexpected '=' on line 55", possibly due to my (mis)use of the proper quotation mark syntax.

global $wpdb;
$itemName = get_the_title( $ID );
$results = $wpdb->get_results($wpdb->prepare("SELECT ID FROM $wpdb->posts where $wpdb->posts.post_title LIKE '%%%s%%'", $wpdb->esc_like("$itemName") AND $wpdb->posts.post_status = 'publish' ORDER BY post_title ASC", OBJECT));
5
  • 1
    Use $wpdb->prepare() codex.wordpress.org/Class_Reference/wpdb#Placeholders
    – bonger
    Commented Jul 30, 2015 at 8:14
  • Do you have multiple posts like: TODAY'S WEATHER IS NICE with apostrophe?
    – Adam
    Commented Jul 30, 2015 at 10:46
  • @bonger - Can that be used within the SQL query itself? There are several ways to remove the single quotation from $itemName but every attempt I've made to use it in the query fails, and breaks all searches, even those that previously worked. Part of the difficulty I'm finding is due to trying to use single quotes within an SQL query. Commented Jul 30, 2015 at 15:45
  • @bonger I tried adding $wpdb->prepare but the outcome is the same. global $wpdb; $itemName = get_the_title( $ID ); $results =$wpdb->get_results("SELECT ID FROM $wpdb->posts where $wpdb->prepare($wpdb->posts.post_title) LIKE $wpdb->prepare('%$itemName%') AND $wpdb->posts.post_status = 'publish' ORDER BY post_title ASC", OBJECT); Commented Jul 30, 2015 at 15:59
  • You put the sql string in the prepare() not vice versa - see @userabuser answer...
    – bonger
    Commented Jul 30, 2015 at 16:39

1 Answer 1

1

Here is an example of performing a LIKE based query while escpaing the value parameter passed to the query:

$results = $wpdb->get_results( 
    $wpdb->prepare( 
        "SELECT post_title, ID from $wpdb->posts WHERE post_title LIKE '%%%s%%'", 
        $wpdb->esc_like("today's weather is nice")
    ) 
);

Update:

Based on your edit, your query is incorrectly formed hence the error, it should be:

global $wpdb;

$itemName = get_the_title($ID);

$results = $wpdb->get_results(
    $wpdb->prepare(
        "
        SELECT ID 
        FROM $wpdb->posts
        WHERE $wpdb->posts.post_title 
        LIKE '%%%s%%' 
        AND $wpdb->posts.post_status = 'publish' 
        ORDER BY post_title ASC
        ", 
        $wpdb->esc_like($itemName)
    )
);
4
  • Thanks. I've edited the original post (first edit) to show the code I'm using as it's now returning an error. I think it's probably an issue with the way I'm using quotation marks. I'm also a little unclear on how the wildcard section of it works. Is this what you meant? Commented Jul 30, 2015 at 21:08
  • I've tried it again with your correctly formed query @userabuser but although it no longer returns an error, it still has the same issue as before; titles containing a single quote or apostrophe don't work even though other ones do. I've checked there are multiple posts with the same title that contain single quotes and also made sure that the query works for those posts when I remove the single quotes from the title (they do). Just in case, I also tried adding str_replace to '$itemName = get_the_title ..etc' without success. I'm not really sure what else there is to try. Commented Jul 31, 2015 at 9:04
  • I'm not really sure what the problem is on your end, as the same query above (either the first example or second) works perfectly fine in my environment. Turn on WP_DEBUG to see if there are any errors being produced that can give some clue as to what is going on. See codex.wordpress.org/Debugging_in_WordPress if you are unfamiliar.
    – Adam
    Commented Jul 31, 2015 at 9:45
  • Also are you sure that the posts you are trying to retrieve some the database have a post_status of publish?
    – Adam
    Commented Jul 31, 2015 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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