0
我必須編輯某人的代碼,並且我不熟悉使用它們的方法查詢帖子。我試圖編輯查詢到各種替代品,但我沒有運氣。我想要更改以下查詢以從名爲'job_cat'的定製分類中選擇數據,其中值等於POST值。代碼如下:WordPress自定義查詢分類標準
$querystr = "
SELECT DISTINCT wposts.*
FROM $wpdb->posts wposts
LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN $wpdb->postmeta wpostmeta2 ON wposts.ID = wpostmeta2.post_id
LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE wp_term_taxonomy.term_id = 94";
// If a job type is set and not equal to nothing or any jobs
if(isset($_POST['search-location']) && $_POST['search-location'] !== "" && $_POST['search-location'] !== "All") {
$querystr .= " AND (wpostmeta2.meta_key = 'region' AND wpostmeta2.meta_value = '".$_POST['search-location']."')";
}
$querystr .= " ORDER BY post_date DESC LIMIT 50";
//Execute
//echo $querystr;
$pageposts = $wpdb->get_results($wpdb->prepare($querystr, OBJECT));
任何幫助將是偉大的!
**警告:** *您的代碼易受SQL注入攻擊!*閱讀[Bobby Tables](http://bobby-tables.com)的故事以獲取更多信息。 – eggyal
我想你使用自定義查詢,而不是WP_Query類。你能確認'wpostmeta'是你餐桌的名字嗎? – pgk
查詢字符串的其餘部分在哪裏?在AND之前你沒有分享這個部分。 – rnevius