我的查询在wordpress中返回null,我不知道为什么

时间:2015-08-27 20:24:41

标签: php wordpress

此查询有什么问题?它总是返回NULL。

$recipes = $wpdb->get_results(
       "SELECT ID FROM ".$wpdb->posts." WHERE post_author = %d AND post_status IN ('draft','publish') AND post_type = 'recipes' ", $current_user->ID
        );

2 个答案:

答案 0 :(得分:1)

get_results将输出类型作为第二个参数。如果你想这样做,你就错过了准备方法。它应该像

 $recipes = $wpdb->get_results($wpdb->prepare ("SELECT ID FROM ".$wpdb->posts." 
 WHERE post_author = %d 
 AND post_status IN ('draft','publish') 
 AND post_type = 'recipes' ", $current_user->ID));

答案 1 :(得分:1)

<强>代码:

global $post;
global $wpdb;

$sel_query = "SELECT id FROM ".$wpdb->prefix."posts WHERE post_author = ".$current_user->ID." AND post_status IN ('draft','publish') AND post_type = 'recipes' ";
$totaldata = $wpdb->get_results($sel_query);

return $totaldata;