I am trying to delete posts of post type using left join but i get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1'
Code:
global $wpdb;
$result = $wpdb->query(
$wpdb->prepare("DELETE posts,pt,pm
FROM wp_posts posts
LEFT JOIN wp_term_relationships pt ON ( pt.object_id = posts.ID AND pt.term_taxonomy_id = %d)
LEFT JOIN wp_postmeta pm ON pm.post_id = posts.ID
WHERE posts.post_type = %s
LIMIT %d
",
456,
'listing',
1
)
);
I am trying to delete posts of post type using left join but i get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1'
Code:
global $wpdb;
$result = $wpdb->query(
$wpdb->prepare("DELETE posts,pt,pm
FROM wp_posts posts
LEFT JOIN wp_term_relationships pt ON ( pt.object_id = posts.ID AND pt.term_taxonomy_id = %d)
LEFT JOIN wp_postmeta pm ON pm.post_id = posts.ID
WHERE posts.post_type = %s
LIMIT %d
",
456,
'listing',
1
)
);
Share
Improve this question
asked Jun 25, 2020 at 13:41
Mohamed OmarMohamed Omar
5191 gold badge5 silver badges17 bronze badges
1 Answer
Reset to default 0You can't use LIMIT directly with a DELETE query, it's invalid syntax. I suggest you get the query working in a tool such as MySQL Workbench to ensure your syntax is correct first then you can migrate it to your WordPress code.