我有一个根据类别检索随机图片集的功能。去年我建立这个网站的时候,它一直在工作。现在我今天测试了这个网站,发现我的随机照片都没有出现。
在我的函数中,我使用WP\\u Query来查询WP DB。WP\\U查询不再返回任何结果。
传递给WP\\u查询的数组的print\\r如下所示:
Array
(
[fields] => ids
[post_status] => inherit
[post_type] => attachment
[post_mime_type] => Array
(
[0] => image/jpeg
[1] => image/png
)
[category_name] => landstrasse+verkehr
[tag] =>
[post__not_in] => Array
(
[0] =>
)
[nopaging] => 1
[posts_per_page] => -1
[orderby] => rand
)
生成的SQL查询如下所示:
SELECT wpq5_12_posts.ID FROM wpq5_12_posts
LEFT JOIN wpq5_12_term_relationships ON (wpq5_12_posts.ID = wpq5_12_term_relationships.object_id)
LEFT JOIN wpq5_12_term_relationships AS tt1 ON (wpq5_12_posts.ID = tt1.object_id)
WHERE 1=1
AND wpq5_12_posts.ID NOT IN (0)
AND (wpq5_12_term_relationships.term_taxonomy_id IN (37) AND tt1.term_taxonomy_id IN (9))
AND (wpq5_12_posts.post_mime_type = \'image/jpeg\' OR wpq5_12_posts.post_mime_type = \'image/png\')
AND wpq5_12_posts.post_type IN (\'post\', \'page\')
AND ((wpq5_12_posts.post_status = \'inherit\'))
GROUP BY wpq5_12_posts.ID ORDER BY RAND()
Select语句错误,因为所需的帖子类型“附件”不在列表中:wpq5\\U 12\\U posts。post\\u键入(“post”,“page”)。
WP\\U查询文档中的任何解决方案都不适用于我。无论我在post\\u类型或post\\u状态中输入什么组合,根据WP\\u查询文档应该工作的组合都不会产生任何结果。
如果替换wpq5\\U 12\\U POST,SQL查询将提供所需的结果。post\\u键入(\'post\',page\'),其中包含:wpq5\\u 12\\u posts。post\\u类型=\'附件\'
SO网友:Georg Ragaz
通过进一步的测试,现在可以肯定的是,WP\\U查询在去年的某个时候被修改了,使其无法用于搜索附件。一旦输入类别或标签等搜索条件,它总是将对帖子类型的搜索替换为:\'。。在(post,page)\'中,或者如果您输入类别和附件的tax\\u查询,则会得到以下SQL:
WP\\U查询数组:
Array
(
[fields] => ids
[post_status] => inherit
[post_type] => attachment
[post_mime_type] => Array
(
[0] => image/jpeg
[1] => image/png
)
[category_name] => bauerndorf
[tax_query] => Array
(
[relation] => OR
[0] => Array
(
[taxonomy] => category
[field] => slug
[terms] => Array
(
[0] => Array
(
[0] => bauerndorf
)
)
[operator] => IN
)
)
[post__not_in] => Array
(
[0] =>
)
[nopaging] => 1
[posts_per_page] => -1
[orderby] => rand
)
生成的SQL。没有搜索分类法的迹象,但是WHERE语句后面的第三行是一个致命的标准:“AND(0=1)”永远不会变为真!!
SELECT wpq5_12_posts.ID FROM wpq5_12_posts
WHERE 1=1
AND wpq5_12_posts.ID NOT IN (0)
AND ( 0 = 1 )
AND (wpq5_12_posts.post_mime_type = \'image/jpeg\' OR wpq5_12_posts.post_mime_type = \'image/png\')
AND wpq5_12_posts.post_type = \'attachment\'
AND ((wpq5_12_posts.post_status = \'inherit\'))
GROUP BY wpq5_12_posts.ID ORDER BY RAND()
我放弃了使用WP\\u查询搜索解决方案。
现在,让我的页面重新工作的唯一解决方案是根据我的需要调整现有的SQL语句并使用wpdb->;收到