You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何WordPress的PODS插件对有效POD调用exists()不返回true?

解决Pods插件中exists()返回false的问题

你的核心问题是对pods()函数的参数用法和exists()方法的作用理解有误:

当你把$params数组作为第二个参数传给pods()时,返回的是条目集合对象,而非单个条目对象。此时调用exists(),它检查的是集合是否包含数据,但集合对象本身不会自动指向具体条目,所以即使数据库中有匹配数据,也会返回false。

两种修正方案

方案1:直接通过ID获取单个条目,再验证作者匹配度

这种方式更直接高效,适合已知条目的ID的场景:

$user_id = 2;
$id = 720;
// 直接传入ID获取单个tftracker条目
$mypod = pods('tftracker', $id);

if (false !== $mypod) {
    // 先确认条目存在,再验证作者是否匹配
    if ($mypod->exists() && $mypod->field('post_author') == $user_id) {
        echo 'exists';
        $fields = ['measure_description', 'unit_type', 'latest_result', 'result_date'];
        echo $mypod->form($fields);
    } else {
        echo 'doesn\'t exist or author mismatch';
    }
}

方案2:使用where条件获取集合,手动定位到具体条目

如果必须通过where条件筛选,需要先调用fetch()定位到集合中的第一个条目,再检查存在性:

$user_id = 2;
$id = 720;
$params = [
    'where' => 't.ID = ' . $id . ' and t.post_author = ' . $user_id,
    'limit' => 1 // 限制只返回一条数据
];
$mypod = pods('tftracker', $params);

if (false !== $mypod) {
    // 定位到集合中的第一个条目
    $mypod->fetch();
    if ($mypod->exists()) {
        echo 'exists';
        $fields = ['measure_description', 'unit_type', 'latest_result', 'result_date'];
        echo $mypod->form($fields);
    } else {
        echo 'doesn\'t exist';
    }
}

关键说明

  • pods($post_type, $id):传入ID时,返回单个条目对象,exists()直接判断该ID对应的条目是否存在。
  • pods($post_type, $params):传入数组参数时,返回集合对象,exists()仅检查集合是否非空,但要操作具体条目,必须先调用fetch()来指向集合中的某一条。

内容的提问来源于stack exchange,提问作者Mark Tait

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 05:10:37