仅当meta_value不为空时,我才能显示帖子
-
-
您正在使用哪个版本的WordPress?Which version of WordPress are you using?
- 0
- 2011-03-02
- MikeSchinkel
-
@MikeSchinkel-对不起,迈克,没看到这个-是3.1.@MikeSchinkel - Sorry Mike, didn't see this — It's 3.1.
- 0
- 2011-03-02
- robalan
-
WP 3.5解决了此问题,并允许您使用其他比较方法WP 3.5 fixes this problem and lets you use a different compare method
- 0
- 2017-12-06
- Erenor Paz
-
7 个回答
- 投票数
-
- 2011-03-02
您好, @Rob:
您无法弄清楚该怎么做的原因是因为它是不可能的,至少在没有SQL的情况下是不可能的.尝试将以下内容添加到主题的
functions.php
文件中:add_filter('posts_where','yoursite_posts_where',10,2); function yoursite_posts_where($where,$query) { global $wpdb; $new_where = " TRIM(IFNULL({$wpdb->postmeta}.meta_value,''))<>'' "; if (empty($where)) $where = $new_where; else $where = "{$where} AND {$new_where}"; return $where; }
如果您有具有空值的自定义
'featured_image'
字段,则上述内容将其过滤掉.如果您还有其他问题,我们将必须查看您的数据是如何解决的.我很好奇的一件事;您如何获得
'featured_image'
的空值?WordPress 3.1中的管理界面会尽力防止您输入空值. 希望这会有所帮助.Hi @Rob:
The reason you can't figure out how to do it is because it's not possible, at least not without resorting to SQL. Try adding the following to your theme's
functions.php
file:add_filter('posts_where','yoursite_posts_where',10,2); function yoursite_posts_where($where,$query) { global $wpdb; $new_where = " TRIM(IFNULL({$wpdb->postmeta}.meta_value,''))<>'' "; if (empty($where)) $where = $new_where; else $where = "{$where} AND {$new_where}"; return $where; }
If you have custom
'featured_image'
fields with empty values the above will filter them out. If you problem is something else, we'll have to see what your data looks like to solve it.One thing I'm curious about; how did you get empty values for
'featured_image'
? The admin UI in WordPress 3.1 does its best to keep you from entering empty values. Hope this helps.-
谢谢上帝,所以不仅仅是我们.我想知道这很好.谢谢@MikeSchinkel-他们真的应该将其放入抄本中...期待解释.谢谢!!Thank god... so it's not just us. That's good to know, I suppose. Thank you @MikeSchinkel - They really ought to put that in the codex... Looking forward to the explanation. Thanks!!
- 0
- 2011-03-02
- robalan
-
@Rob-所以我不懂3.0的知识,现在我正在3.1中对其进行测试,它似乎确实有效.我有两篇文章,一篇有一个"featured_image"自定义字段,您的查询工作正常.你在找什么?您的查询是否有机会加载确实具有"featured_image"自定义字段但该字段的值为空的帖子?@Rob - So I was going off my 3.0 knowledge and now I'm testing it in 3.1 and it does seem to work. I have two posts and one with a `featured_image` custom field and your query works fine. What are you finding? Is there a chance your query is loading posts that do have a `featured_image` custom field but where the value for that field is empty?
- 0
- 2011-03-02
- MikeSchinkel
-
@MikeSchinkel-开玩笑吗?是的,这正是它的作用-每个帖子都具有featured_image字段,但并没有全部设置好.无论如何,它正在加载所有帖子.@MikeSchinkel - No kidding? Yes, that's exactly what it's doing — every post has the featured_image field, they're just not all set. It's loading up all posts anyway.
- 0
- 2011-03-02
- robalan
-
@Rob-查看我的最新答案.@Rob - See my updated answer.
- 0
- 2011-03-02
- MikeSchinkel
-
@MikeSchinkel-谢谢!在查询中还设置了post_type之后,它似乎运行得很好.空值是有目的的-并非每个帖子都有此特色图片(我知道帖子的缩略图,这对本网站来说不是一个很好的选择).非常感谢!@MikeSchinkel - Thank you! It seems to be working perfectly, after setting the post_type in the query also. The empty values are purposeful — not every post has this featured image (and I know about the post thumbnail, it's just not a good option for this site). Thanks so much!
- 0
- 2011-03-02
- robalan
-
@MikeSchinkel-我的立场是正确的-它可以在我想要的查询中使用,但会在其他所有地方引起404错误.单个帖子页,存档...:o \有什么想法吗?@MikeSchinkel - I stand corrected - it works on the queries that I wanted it to, but it's causing 404's everywhere else. Single post pages, archives... :o\ Any ideas?
- 0
- 2011-03-02
- robalan
-
在上述问题的代码示例中,尝试在调用WP_Query()之前放置add_filter()调用,然后在调用wp_reset_query(之前)放置remove_filter('posts_where','yoursite_posts_where');`.)`.In the code sample in your question above, try putting the `add_filter()` call just before your `WP_Query()` call, then put `remove_filter('posts_where','yoursite_posts_where');` just before you call `wp_reset_query()`.
- 0
- 2011-03-02
- Dougal Campbell
-
该代码检查所有自定义字段,以查看其中是否为空?如果我只希望查询仅检查一个特殊字段并查看该字段是否为空(不管其他字段是什么),该怎么办?That code checks ALL custom fields to see if ANY of them are empty? What if I want the query to only check for one special field and see if that is empty no matter what the other ones are?
- 0
- 2011-05-31
- Jens Törnell
-
- 2011-03-04
这似乎可以将值添加到查询中,但不确定是否会提取有效结果.
'meta_query' => array( array( 'key' => 'some_key', 'value' => array(''), 'compare' => 'NOT IN' ) )
没有时间创建用于测试结果的字段,但是我一直在观察我今天使用的查询,并且注意到
NOT IN
会很高兴地采用一个空数组.This seems to work for getting the value into the query, not sure about whether it pulls valid results though..
'meta_query' => array( array( 'key' => 'some_key', 'value' => array(''), 'compare' => 'NOT IN' ) )
Not had time to create fields to test the results, but i've been watching queries i've worked with today and noticed
NOT IN
will happily take an empty array.-
我知道这是一个老答案,但是对于那些尝试这种方法的人来说,它可以使用''compare'=>'NOT LIKE'`而不是''NOT IN`'I know this is an old answer, but for those trying this apparoach, it works with `'compare' => 'NOT LIKE'` instead of `'NOT IN`'
- 0
- 2012-07-11
- handsofaten
-
使用!=肯定比不使用in或not like更有效.与http://wordpress.stackexchange.com/a/10286/32863相同(关于元键,但原理相同)Surely more efficient to use != instead of not in or not like. Same as http://wordpress.stackexchange.com/a/10286/32863 (that is about meta keys, but same principle)
- 3
- 2015-09-09
- Adam
-
更干净的解决方案:就像亚当已经说过的那样.用于: ''value'=>'', '比较'=>'!='`Even cleaner solution: like Adam stated already. Is to use: `'value' => '', 'compare' => '!=' `
- 5
- 2018-04-20
- Martijn van Hoof
-
如果需要检查以确保它不是一个空数组...''value'=> array('',array(),serialize(array())),'compare'=>'NOT IN'`If you need to check to make sure it's not an empty array... `'value' => array('', array(), serialize(array())), 'compare' => 'NOT IN'`
- 0
- 2018-08-14
- StephanieQ
-
- 2017-12-06
这是一个古老的问题,但似乎Wordpress已修复此"缺失功能":现在,根据 Wordpress Codex 可以像这样检查元密钥的存在(或不存在)
'meta_query' => array( array( 'key' => 'featured_image', 'compare' => 'EXISTS', //or "NOT EXISTS", for non-existance of this key ) )
自WP>=3.5起可用.
This is an old question, but it seems Wordpress has fixed this "missing feature": now, according to Wordpress Codex is possible to check for existence (or non-existence) of the meta key, like this
'meta_query' => array( array( 'key' => 'featured_image', 'compare' => 'EXISTS', //or "NOT EXISTS", for non-existance of this key ) )
This is available as of WP >= 3.5.
-
" EXISTS"将显示空值,这不是我们想要的.就我的测试而言,最好的解决方案是''value'=>'','compare'=>'!='`.`EXISTS` will show empty values which is not what we're after here. The best solution is `'value' => '', 'compare' => '!=' ` as far as my testing goes.
- 0
- 2019-08-09
- Ben
-
@Ben这正是OP尝试的方法,但似乎没有成功.我添加了解决方案,以确保通过搜索基于meta_key的存在来检索帖子的方法的人能够找到它.由于元数据的值可以是"某物","空"或"空"(以防元数据不存在),因此最好的解决方案是将两个选项与"与"关系合并@Ben That's exactly what the OP tried, but without any success, it seems. I added my solution to make sure people passing by searching for a method to retrieve posts based on existence of meta_key could find it. Since the value of a meta can be "something", "empty" or "null" (in case the meta doesn't exist), probably the best solution would be to merge the two options with an "AND" relation
- 2
- 2019-08-09
- Erenor Paz
-
- 2018-01-10
这是对我有用的查询.与2011年t31os的答案中的比较非常相似,但是由于meta键/值只是一个简单的文本字符串,因此不必是meta_query数组.
$args = array( 'posts_per_page' => 5,//replaced 'showposts' in version 2.1 'meta_key' => 'featured_image', 'meta_value' => array(''), 'meta_compare' => 'NOT IN' );
出于任何原因,使用'meta_value'=>''和'meta_compare'=>'!='或'meta_compare'=>'不就像一样,我仍然可以撤消所有帖子,但是这可能与我使用"高级自定义字段(ACF)插件"创建了元值有关.
This is the query that worked for me. Very similar to the comparison in t31os's answer from 2011, but because the meta key/value is just a simple textstring, it doesn't need to be a meta_query array.
$args = array( 'posts_per_page' => 5,//replaced 'showposts' in version 2.1 'meta_key' => 'featured_image', 'meta_value' => array(''), 'meta_compare' => 'NOT IN' );
For whatever reason, using 'meta_value' => '' and 'meta_compare' => '!=' or 'meta_compare' => 'NOT LIKE' still pulled all posts for me, but it probably has something to do with the fact that I created my meta value using the Advanced Custom Fields (ACF) plugin.
-
- 2011-03-03
此问题在WP 3.2-alpha中已解决:
This is fixed in WP 3.2-alpha:
-
- 2014-01-24
我想念什么吗?
<?php $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'meta_key' => "featured_image" ); $the_query = new WP_Query( $args ); ?>
不是那样吗?
Am i missing something?
<?php $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'meta_key' => "featured_image" ); $the_query = new WP_Query( $args ); ?>
Won't that do it?
-
编辑:从抄本中:```$ query=new WP_Query('meta_key=featured_image');``看这里:http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_ParametersEdit: from the codex: ```$query = new WP_Query( 'meta_key=featured_image' );``` look here:http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
- 0
- 2014-01-24
- Infinity Media
-
首先,您始终可以[编辑]问题或答案,而无需添加评论.第二:不要"滥用"答案而不是评论.First, you can always [edit] a question or answer instead of adding a comment. Second: Don't **misuse** answers instead of comments.
- 0
- 2014-01-25
- kaiser
-
如果您有新问题,请单击[问问题](http://wordpress.stackexchange.com/questions/ask)按钮.如果它有助于提供上下文,请包括指向该问题的链接.If you have a new question, please ask it by clicking the [Ask Question](http://wordpress.stackexchange.com/questions/ask) button. Include a link to this question if it helps provide context.
- 0
- 2014-01-25
- kaiser
-
@kaiser-在我看来他在回答.他没有问他的代码是否有效,但是我想用他认为会起作用的方式讽刺地回答这个问题.@kaiser - seems to me like he was answering. He's not asking if his code is valid, but I suppose sort of sarcastically answering the question with what he believes will work.
- 0
- 2016-09-05
- Nathan
-
@Nathan这就是意见.@Nathan This is what comments are for.
- 0
- 2016-09-05
- kaiser
-
@kaiser,评论用于发布答案和代码吗?您如何在评论中发布代码?@kaiser, Comments are for posting answers and code? How do you post code in a comment?
- 0
- 2016-09-05
- Nathan
-
@Nathan我指的是答案的讽刺部分.该代码应该是一个答案-包括一些解释.@Nathan I was referring to the sarcastic part of the answer. The code should be an answer – including some explanation.
- 0
- 2016-09-05
- kaiser
-
WordPress中有一个错误,但现在已修复.这是正确的方法.There was a bug in WordPress but now it's fixed. This is the correct way to do it.
- 0
- 2016-12-04
- Ryan Taylor
-
已经有三个人试图解决这个问题,而我们提出的目标是零.我只想显示meta_key'featured_image'中具有值的帖子.
所以...如果'featured_image'不为空,请显示该帖子.这是代码:
我们已经尝试了几乎所有可以想到的组合,已弃用的meta_ *选项,query_posts,get_posts,而不是WP_Query ...什么也没有.打印选择语句,不显示任何元值字段.它存在-用于帖子(每个帖子),并且存在于数据库中.
我们现在已经看到有关该主题的每篇文章,包括:
query_posts并且仅在自定义字段不为空的情况下显示结果
http://scribu.net/wordpress/advanced-metadata-queries.html
Zilch.请帮助...