我添加了一個自定義的Visual Composer元素,並希望輸出查詢結果。但是,當我輸出這個查詢(1)時,它會給出其他結果,然後當我輸出這個查詢(2)。WordPress的SQL查詢不同的結果
// declared vars
global $wpdb;
$nn = '{{ post_data:ID }}';
$nn1 = (string)$nn;
1) $results2 = $GLOBALS['wpdb']->get_results("SELECT guid FROM wp_posts WHERE post_parent = 526 AND guid like '%pdf'", OBJECT);
2) $results2 = $GLOBALS['wpdb']->get_results("SELECT guid FROM wp_posts WHERE post_parent = ".$nn1." AND guid like '%pdf'", OBJECT);
當我輸出的526字符串(查詢1)它給出正確的結果,但是我所要的輸出是用var,因爲每一個崗位都有其唯一的ID。
我還試圖把VAR在一個字符串,因此它不被視爲一個POST_ID
$nn = '{{ post_data:ID }}';
$nn1 = (string)$nn;
任何想法的地方,這是失敗的?
我已經嘗試在此期間:
$nn1 = (string)$nn;
$nn2 = strval($nn);
$nn3 = settype($nn, "string");
$results1 = $GLOBALS['wpdb']->get_results("SELECT guid FROM wp_posts WHERE post_parent = ".$nn1." AND guid like '%pdf'", OBJECT);
$results2 = $GLOBALS['wpdb']->get_results("SELECT guid FROM wp_posts WHERE post_parent = ".$nn2." AND guid like '%pdf'", OBJECT);
$results3 = $GLOBALS['wpdb']->get_results("SELECT guid FROM wp_posts WHERE post_parent = ".$nn3." AND guid like '%pdf'", OBJECT);
$results4 = $GLOBALS['wpdb']->get_results("SELECT guid FROM wp_posts WHERE post_parent = %d AND guid like '%pdf'", $nn3, OBJECT);
上述所有正在返回一個空字符串,這意味着仍然有一些錯誤使用var
當我呼應整個字符串它返回正確的MySQL查詢:
echo ("SELECT guid FROM wp_posts WHERE post_parent = ".$nn."");
//output
SELECT guid FROM wp_posts WHERE post_parent = 526;
當我執行該查詢,它給了我正確的解決方案:
$results6 = $GLOBALS['wpdb']->get_results("SELECT guid FROM wp_posts WHERE post_parent = 526", OBJECT);
// output
[6]=>object(stdClass)#8030 (1) {
["guid"]=>
string(69) "http://blabla.pdf"}
格爾茨
我的意思是鍵入= 526(整數) - 編輯問題 –
你的'$ nn1'變量字面上包含'{{post_data:ID}}'而不是數字。 – jeroen
如何將{{post_data:ID}}放入數字中? –