如何对仅发布的帖子进行添加和SQL查询

时间:2018-06-21 作者:acrane

我有一个菜单项上的通知,需要更改通知上的编号。我错过了一个只显示“已发布”帖子的论点。现在它也在把所有的东西都拉进去。这是我现在拥有的。需要向其中添加已发布的零件。

    $prepare_string = "SELECT DISTINCT ID FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id ) LEFT JOIN $wpdb->postmeta AS mt1 ON ( $wpdb->posts.ID = mt1.post_id ) WHERE ( ( $wpdb->postmeta.meta_key = \'event_date\' AND $wpdb->postmeta.meta_value >= %d ) OR ( ( $wpdb->postmeta.meta_key = \'event_date\' AND $wpdb->postmeta.meta_value < %d ) AND ( mt1.meta_key = \'end_date\' AND mt1.meta_value >= %d ) ) OR ( $wpdb->postmeta.meta_key = \'event_date\' AND $wpdb->postmeta.meta_value = \'\' ) )";

1 个回复
最合适的回答,由SO网友:David Sword 整理而成

您应该可以通过添加AND $wpdb->posts.post_status = \'publish\' 在最后。

$prepare_string = "
    SELECT DISTINCT ID 
    FROM        $wpdb->posts 
    LEFT JOIN   $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id ) 
    LEFT JOIN   $wpdb->postmeta AS mt1 ON ( $wpdb->posts.ID = mt1.post_id ) 
    WHERE       ( ( $wpdb->postmeta.meta_key = \'event_date\' AND $wpdb->postmeta.meta_value >= %d ) 
    OR          ( ( $wpdb->postmeta.meta_key = \'event_date\' AND $wpdb->postmeta.meta_value < %d ) 
    AND         ( mt1.meta_key = \'end_date\' AND mt1.meta_value >= %d ) ) 
    OR          ( $wpdb->postmeta.meta_key = \'event_date\' AND $wpdb->postmeta.meta_value = \'\' ) )
    AND         $wpdb->posts.post_status = \'publish\'
";

结束