最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wpdb - Limit left join

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论