I have problem with wpdb prepare and sql query. If i run the query directly thru wpdb->get_results it's working fine. Here it is:
$alabala_sql = $wpdb->prepare(
"
SELECT *
FROM $wpdb->posts p
INNER JOIN $wpdb->term_relationships tr
ON p.ID = tr.object_id
INNER JOIN $wpdb->term_taxonomy tt
ON tr.term_taxonomy_id=tt.term_taxonomy_id
inner join $wpdb->terms t
on t.term_id=tt.term_id and t.slug='$curPageId'
WHERE
p.post_title LIKE '$getChar%'
AND p.post_type = 'filmi-i-serialii'
AND p.post_status = 'publish';
"
);
/*Make sql query to take posts*/
$eventsPosts = $wpdb->get_results($alabala_sql);
But it doesn't work. Also i tried that way:
$alabala_sql = $wpdb->prepare(
"
SELECT *
FROM $wpdb->posts p
INNER JOIN $wpdb->term_relationships tr
ON p.ID = tr.object_id
INNER JOIN $wpdb->term_taxonomy tt
ON tr.term_taxonomy_id=tt.term_taxonomy_id
inner join $wpdb->terms t
on t.term_id=tt.term_id and t.slug=$s
WHERE
p.post_title LIKE $s%
AND p.post_type = 'filmi-i-serialii'
AND p.post_status = 'publish';
", $curPageId, $getChar
);
/*Make sql query to take posts*/
$eventsPosts = $wpdb->get_results($alabala_sql);
But again it didn't worked. Please tell me where is my mistake ? Both vars are strings. Regards.
slug=$s
must beslug=%s
andLIKE $s%
must beLIKE %s
.SELECT * FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id=tt.term_taxonomy_id inner join $wpdb->terms t on t.term_id=tt.term_id and t.slug=%s WHERE p.post_title LIKE %s% AND p.post_type = 'filmi-i-serialii' AND p.post_status = 'publish'; ", $curPageId, $getChar
Didn't work.sprintf()
works. You cannot use a plain%
in a string evaluated bysprintf
. Use$getChar . '%'
instead.SELECT * FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id=tt.term_taxonomy_id inner join $wpdb->terms t on t.term_id=tt.term_id and t.slug=$curPageId WHERE p.post_title LIKE $getChar. '%' AND p.post_type = 'filmi-i-serialii' AND p.post_status = 'publish'; ",$curPageId, $getChar
It doesnt worked again.%
in that string.