douxigai8757 2018-08-09 16:22
浏览 51
已采纳

Wordpress:wpdb准备条件

I'm trying to use sql->prepare with many strings concatenation.

In my code below, I try to call a function which will return me an array of get_post from $wpdb->prepare.

function test($array){
    $sqlprep = "SELECT SQL_CALC_FOUND_ROWS  p.ID 
        FROM {$wpdb->prefix}posts  p
        LEFT JOIN {$wpdb->prefix}postmeta m ON m.post_id = p.ID
        LEFT JOIN {$wpdb->prefix}term_relationships r ON (p.ID = r.object_id)  
        LEFT JOIN {$wpdb->prefix}term_relationships r1 ON (p.ID = r1.object_id) 
        LEFT JOIN {$wpdb->prefix}term_relationships r2 ON (p.ID = r2.object_id) 
        LEFT JOIN {$wpdb->prefix}term_taxonomy tt ON tt.term_taxonomy_id = r.term_taxonomy_id
        LEFT JOIN {$wpdb->prefix}terms t ON t.term_id = tt.term_id AND t.term_id = r.term_taxonomy_id
        WHERE 1=1";

    if(isset($array['post_type']) && !empty($array['post_type']){
        $sqlprep .= " AND p.post_type = '".$array['post_type']."'";
    }
    if(isset($array['post_type']) && !empty($array['post_type']){
        $sqlprep .= " AND p.post_type = '".$array['post_status']."'";
    }

    $sql = $wpdb->prepare($sqlprep);
    $ids = $wpdb->get_col($sql);
    return array_map('get_post', $ids)
}

and the value is an array which is :

$args=array(
    'post_type' => 'post',
    'post_status' => 'published',
);

When I try to print_r($sql) to get the query, it always just stop till WHERE 1=1.

Can string concatenation be used for wpdb prepare?

  • 写回答

1条回答 默认 最新

  • duanniu4106 2018-08-09 16:43
    关注

    Pay attention to the use of the properties in the $wpdb object and the use of the prepare method, furthermore there is an issue with the column name for the post_status:

    function test($array){
        $sql = "SELECT SQL_CALC_FOUND_ROWS  p.ID 
            FROM {$wpdb->posts}  p
            LEFT JOIN {$wpdb->postmeta} m ON m.post_id = p.ID
            LEFT JOIN {$wpdb->term_relationships} r ON (p.ID = r.object_id)  
            LEFT JOIN {$wpdb->term_relationships} r1 ON (p.ID = r1.object_id) 
            LEFT JOIN {$wpdb->term_relationships} r2 ON (p.ID = r2.object_id) 
            LEFT JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = r.term_taxonomy_id
            LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id AND t.term_id = r.term_taxonomy_id
            WHERE 1=1";
    
        if(isset($array['post_type']) && !empty($array['post_type']){
            $sql .= $wpdb->prepare(" AND p.post_type = %s", $array['post_type']);
        }
        if(isset($array['post_type']) && !empty($array['post_type']){
            $sql .= $wpdb->prepare(" AND p.post_status = %s", $array['post_status']);
        }
    
        $ids = $wpdb->get_col($sql);
        return array_map('get_post', $ids)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏