query_posts,仅在自定义字段不为空时显示结果
2 个回答
- 投票数
-
- 2011-02-24
尝试以下代码:
$args = array( 'posts_per_page' => '10', 'post_type' => 'programmes', 'meta_key' => 'popularityfig', 'meta_value' => '', 'meta_compare' => '!=', 'order' => 'DESC' );
您可能需要在代码中注意两个参数:
meta_value
和meta_compare
.将meta_compare
与运算符!=
一起使用将排除元值为空的帖子.Try this code:
$args = array( 'posts_per_page' => '10', 'post_type' => 'programmes', 'meta_key' => 'popularityfig', 'meta_value' => '', 'meta_compare' => '!=', 'order' => 'DESC' );
There're 2 arguments you might want to note in the code:
meta_value
andmeta_compare
. Usingmeta_compare
with operator!=
will exclude posts with empty meta value.-
谢谢,我现在可以在法典上看到吗?为什么在不确定之前看不到它!!再次感谢.Thanks, I can see that on codex now?? Why I couldn't see it before I'm not sure!! Thanks again.
- 0
- 2011-02-24
- erichmond
-
无论出于何种原因,这都会使我的所有帖子变得毫无价值.取而代之的是,我使用了'meta_value'=> array('')和'meta_compare'=>'NOT IN'并成功了,我使用高级自定义字段(ACF)插件创建了元值,它只是一个文本字段.For whatever reason, this kept pulling all of my posts regardless of value. Instead, I used 'meta_value' => array('') and 'meta_compare' => 'NOT IN' and that worked.I created my meta value with the Advanced Custom Fields (ACF) plugin and it was just a text field.
- 0
- 2018-01-10
- Tessa
-
- 2011-03-02
尚不清楚上面的答案是否真的对您有用@erichmond,但绝对不是我想要的.虽然找到了解决方案.请参阅:如何显示仅在meta_value不为空的情况下发布
It's not clear if the answer above actually worked for you @erichmond, but it definitely did not for me. Found a solution though. See: How can I show posts only if meta_value is not empty
如何
query_posts
,并且仅在自定义字段不为空或具有值的情况下显示结果.我想在自定义字段中输入URL,并且仅在存在URL时才显示这些页面?
当前代码,但我想不通: