简体   繁体   中英

My query returns null in wordpress and I don't know why

What's wrong with this query? It always returns 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
        );

get_results takes output type as second parameter. You're missing prepare method if you want to do it like this. It should be something like

 $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));

Code:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM