查询所有不存在元键的帖子
-
-
您正在使用哪个版本的WordPress?What version of WordPress are you using?
- 0
- 2013-01-12
- s_ha_dum
-
嗨,抱歉.我正在使用v3.5Hi,sorry for the omission. I am using v3.5
- 0
- 2013-01-12
- JordanBel
-
好像在3.5中添加了该类型的查询(compare设置为NOT EXISTS),所以据我所知它应该可以正常工作.不过,通过自定义SELECT查询很容易做到这一点.It seems as though that type of query (with compare set to NOT EXISTS) was added in 3.5, so it should work as it is, as far as I can see. It would be easy to do it via custom SELECT query, though...
- 0
- 2013-01-12
- Tomas Buteler
-
谢谢,我会尝试使用选择.我必须了解要查询哪些表以及如何使查询符合:(Thanks I will try using select. I must learn before which tables to query and how to conform the query though :(
- 0
- 2013-01-12
- JordanBel
-
很奇怪.我找不到该代码的问题,而您使用的是3.5+,这就是我问的原因.您是否实际上已经查看了数据库以确认是否按照您认为的方式插入了数据?Very strange. I can't spot a problem with that code and you are using 3.5+, which is why I asked. Have you actually looked at the database to confirm that your data is being inserted the way you think it is?
- 0
- 2013-01-13
- s_ha_dum
-
您是否尝试过" EXISTS"?您提到过" EXIST",但我相信比较只接受以" S"结尾的" EXISTS".Have you tried 'EXISTS'? You mentioned 'EXIST' but I believe compare only accepts 'EXISTS' with an 'S' on the end.
- 0
- 2013-01-13
- mikegertrudes
-
你好谢谢.不幸的是,EXISTS也不起作用.我将转到我认为的查询选项.Hi, thanks. Unfortunately EXISTS didn't work either. I will go to the query option I think.
- 0
- 2013-01-14
- JordanBel
-
3 个回答
- 投票数
-
- 2013-01-16
我对此进行了更多测试,并且坦白地说找不到它不起作用的原因(除非上面的代码只是摘录,并且真实的代码适合下面的示例).但是,我确实发现了一些可能会引导您朝正确方向发展的事情.
1)本身,此元查询等效于" colors IS NULL",即它将返回postmeta表中未设置该键的帖子.上面显示的就是这种情况,它应该可以工作.
'meta_query' => array( array( 'key' => 'colors', 'compare' => 'NOT EXISTS' // this should work... ), )
2)在WordPress 3.9之前,将" relation"索引建立为" OR"会改变这种情况.它返回相反的结果.不要问我为什么.在执行多个元查询时,这一点尤其重要.这意味着最初不可能对"颜色"键设置为"蓝色"(或其他设置)或根本没有设置的帖子进行查询.下面的查询将忽略第一个条件,仅返回与第二个条件匹配的条件.
'meta_query' => array( 'relation' => 'OR', array( 'key' => 'colors', 'compare' => 'NOT EXISTS' // doesn't work ), array( 'key' => 'colors', 'value' => 'blue' ) )
3)但是,如果我们设置"值",我们可以欺骗WordPress使用第一个条件.它不需要相关的值(据我所知,它会被忽略),但是需要设置,以使
NOT EXISTS
条件生效'meta_query' => array( 'relation' => 'OR', array( 'key' => 'colors', 'compare' => 'NOT EXISTS', // works! 'value' => '' // This is ignored, but is necessary... ), array( 'key' => 'colors', 'value' => 'blue' ) )
直到WordPress 3.9为止都是如此.如果您仍在使用旧版本,这是一个可行的解决方法.
I did some more testing with this, and honestly can't find a reason it wouldn't work (unless the code above is just a snippet and the real code fits on my examples below). I did, however, discover a couple of things that might lead you in the right direction.
1) By itself, this meta query is the equivalent of "colors IS NULL", i.e. it'll return the posts which don't have that key set in the postmeta table. This is the case shown above, and it should've worked.
'meta_query' => array( array( 'key' => 'colors', 'compare' => 'NOT EXISTS' // this should work... ), )
2) Prior to WordPress 3.9, establishing the 'relation' index to 'OR' changes this condition. It returns the opposite. Don't ask me why. This is especially important when doing multiple meta queries. That means that is not initially possible to do a query for posts that have 'colors' key set to 'blue' (or whatever) or not set at all. The query below will ignore the first condition and return only those that match the second condition.
'meta_query' => array( 'relation' => 'OR', array( 'key' => 'colors', 'compare' => 'NOT EXISTS' // doesn't work ), array( 'key' => 'colors', 'value' => 'blue' ) )
3) However, we can fool WordPress into using the first condition if we set the 'value'. It doesn't need a relevant value (it's ignored, as far as I know), but it needs to be set in order for the
NOT EXISTS
condition to have any effect.'meta_query' => array( 'relation' => 'OR', array( 'key' => 'colors', 'compare' => 'NOT EXISTS', // works! 'value' => '' // This is ignored, but is necessary... ), array( 'key' => 'colors', 'value' => 'blue' ) )
This was true up until WordPress 3.9. If you're still using an older version, this is a viable workaround.
-
谢谢!很抱歉造成您的延迟.我最终使用了一个查询,但是接下来的几个小时我将测试您的解决方案,以便我可以切换回去,如果可以的话,我们可以提供其他帮助.我会尽快通知您.再次感谢Thanks! And sorry for the delay. I ended up using a query, but I will be testing your solution in the following hours so I can switch back and maybe if this work we can help some other. I will let you know as soon as I can check it. Thanks again
- 0
- 2013-01-17
- JordanBel
-
写得很好,并确认添加空值将返回预期结果.我想说这是无意的,值得在trac.wordpress.org上看看是否已经有一张票,如果没有,这是可复制的.Well written and confirmed that adding an empty value returns expected results. I'd say it's unintentional, may be worth a look at trac.wordpress.org to see if there's already a ticket, if not, this is reproducible.
- 0
- 2013-03-06
- Taylor Dewey
-
感谢您对WP技巧的出色解释和解决方案:)确实花了一些时间才能到达这里-但现在我要单击upvote至少10次(如果可以的话).Thanks for the great explanation and solution to trick WP :) Did take some time to get here - but now I want to click upvote for at least 10 times (if only I could ;))
- 0
- 2013-03-21
- lorem monkey
-
如果我使用compare EXISTS,不幸的是,在新版本的WP中未忽略值(在4.2.2中进行了测试)If I use compare EXISTS, value is unfortunately not ignored in newer versions of WP (tested in 4.2.2)
- 0
- 2015-07-01
- Igor Jerosimić
-
注意`orderbymeta_key`也可能在您的查询中!WordPress还将对meta orderby进行" WHEREmeta_key=value"的硬编码!Be aware of an `orderby meta_key` that may also be in your query! Wordpress will hardcode `WHERE meta_key = value` for a meta orderby as well!
- 0
- 2016-04-08
- Chizzle
-
WP 3.9中修复了要求您指定值的" EXISTS"和" NOT EXISTS""错误"The `EXISTS` and `NOT EXISTS` "bug" which required you to specify a value, was fixed in WP 3.9
- 11
- 2016-04-15
- trex005
-
trex005是正确的:"((注意:由于错误#23268,在NOT EXISTS比较之前,必须使用值才能在3.9之前正常工作.您必须为value参数提供一些字符串.空字符串或NULL将不起作用.但是,任何其他字符串将解决问题,并且在使用NOT EXISTS时不会在您的SQL中显示.需要灵感吗?"错误#23268"如何.)"trex005 is right: "(Note: Due to bug #23268, value is required for NOT EXISTS comparisons to work correctly prior to 3.9. You must supply some string for the value parameter. An empty string or NULL will NOT work. However, any other string will do the trick and will NOT show up in your SQL when using NOT EXISTS. Need inspiration? How about 'bug #23268'.)"
- 0
- 2017-04-29
- jave.web
-
- 2015-01-09
使用自定义查询,这对我有用:
SELECT * FROM wp_posts as posts WHERE posts.post_type = 'post' AND NOT EXISTS ( SELECT * FROM `wp_postmeta` WHERE `wp_postmeta`.`meta_key` = "your_meta_key" AND `wp_postmeta`.`post_id`=posts.ID )
Using a custom query, this worked for me:
SELECT * FROM wp_posts as posts WHERE posts.post_type = 'post' AND NOT EXISTS ( SELECT * FROM `wp_postmeta` WHERE `wp_postmeta`.`meta_key` = "your_meta_key" AND `wp_postmeta`.`post_id`=posts.ID )
-
- 2020-01-09
这对我有用.
'meta_query' => array( 'relation' => 'OR', array( 'key' => 'colors', 'compare' => 'NOT EXISTS' // doesn't work ), array( 'key' => 'colors', 'value' => 'blue' ) )
This worked for me.
'meta_query' => array( 'relation' => 'OR', array( 'key' => 'colors', 'compare' => 'NOT EXISTS' // doesn't work ), array( 'key' => 'colors', 'value' => 'blue' ) )
我正在尝试查询所有不存在特定
meta_key
的帖子,然后创建它.我在查找这些帖子时遇到问题,因为我正在测试的查询似乎不起作用.
这是我用来尝试获得这些帖子的代码:
如果没有键为
colors
的帖子,则不返回任何内容,但只要返回键为colors
的帖子,则返回它们的colors
.密钥存在(与我需要的相反).我尝试使用EXIST
代替,但是没有运气.如果有人可以像我需要的那样向我提示创建查询的正确方法,
谢谢!