duanhun3273 2012-10-04 11:36
浏览 19
已采纳

我需要检查当前用户是否为自定义帖子类型的作者

I want to write a conditional statement (in pseudo below to explain)

IF current logged in user is 'Author' to a custom-post-type called 'farmers' {
// Do something
} else {
// Do something else
}

My code effort so far is

<?php
// Global variable for current user ID
$user_ID;

// Create a new WP query object           
$my_query = new WP_Query( array(
'post_type' => 'farmers',
'author' => $user_ID
));

// Get all the current user posts from the query object
$posts = $my_query->posts;

IF $posts = 0 {
// Do something
} else {
// Do Something else
} ?>

User can have the role "Author" in the system, but not necessarily have any posts. So I need to check the condition that this Author actually has a post. I am just not sure how to write that IF statement at the end of the code and looking for some help.

Thanks

  • 写回答

3条回答 默认 最新

  • dtny30176 2012-10-04 11:42
    关注

    Use something like this

    $query="select * from posts where type='farmers'";
    $result=mysql_query($query);
    $valid=false;
    if(mysql_num_rows($result)>0){
    $valid=true;
    }
    
    
    if($_SESSION["id"]==$posts["id"]){
    //your code
    }else{
    //your code
    }
    

    or

    if(mysql_num_rows($posts)>0){
    // user have post
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看