首页 > 解决方案 > Wordpress:php strpos 不适用于 WP_Query

问题描述

好的,所以基本上我有这个代码:

<?php $query = new WP_Query( array( 'meta_key' => 'usp-custom-1', 'meta_value' => 
get_permalink() ) );

echo $query->found_posts; 
        
if (strpos($query, "0") !== false) {
echo "yes";
} else {
echo "no";
}
?>

我想要实现的是,当 WP_Query 输出(找到的帖子)为 0 时,它会说是,否则不会。

但它不起作用。

不管数字多少,它总是说是。

我想我在这里遗漏了一些东西。

不是码农,

迫切需要帮助。

标签: phpwordpress

解决方案


只需与$query->found_posts而不是使用比较strpos

<?php 
$query = new WP_Query( 
    array( 
        'meta_key' => 'usp-custom-1', 
        'meta_value' => get_permalink() 
    ) 
);

echo $query->found_posts; 
        
if (0 === $query->found_posts ) {
    echo "yes";
} else {
    echo "no";
}
?>

推荐阅读