我正在使用以下代码从我的数据库中检索当前用户的帖子ID。不幸的是它似乎没有用。
global $wpdb;
$user_ID = get_current_user_id();
$count = $wpdb->query('SELECT ID From'. $wpdb->posts . 'WHERE post_author = "'.$user_ID.'" AND post_type = "job_listing"');
echo $count . $user_ID. "comments"; ?>
$ user_ID和"评论"显示,但$ count没有。我在SQL上运行了这个查询,它可以工作。
有什么建议吗?
答案 0 :(得分:0)
空格正在抛弃查询,正确的查询是
$count = $wpdb->query('SELECT ID From '. $wpdb->posts . ' WHERE post_author = "'.$user_ID.'" AND post_type = "job_listing"');
归功于@larsAnders
答案 1 :(得分:-2)
<?php global $wpdb;
$user_ID = get_current_user_id();
$sql='SELECT count(*) From '. $wpdb->posts . ' WHERE post_author = "'.$user_ID.'" AND post_type = "job_listing"';
$count = $wpdb->get_var($sql);
echo $count ."posts by " $user_ID. "comments";?>//give the total post count
对于由currentrnt用户发布的帖子ID
$sql='SELECT * From '. $wpdb->posts . ' WHERE post_author = "'.$user_ID.'" AND post_type = "job_listing"';
$totalposts = $wpdb->get_results($sql);
foreach($totalpost as $userpost):
$post_id=$userpost[ID];
//if required you can get other field too
endforeach
尝试这个