wordpress query_posts 查看元信息
我希望使用 query_posts 运行一个查询,让我查看 meta_fields。 我之前已经这样做过一次并达到了我想要的结果,但现在由于某种原因在方程中添加一个额外的字段不会返回任何结果。
下面的查询返回所有附件
,其中lottery_year
等于$y
,显然$y
可以是 2011
或更早版本。
现在我的下一步是引入另一个名为 photo_technique
的 meta_field
并为其赋予一个值。
问题,我该怎么做?
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE ($wpdb->postmeta.meta_key = 'lottery_year' AND $wpdb->postmeta.meta_value = $y)
AND $wpdb->posts.post_type = 'attachment'
ORDER BY $wpdb->postmeta.meta_value ASC
LIMIT 100
";
I am looking to run a query with query_posts that lets me look at meta_fields.
I have done this once before and achieved the result I wanted but now for some reason adding an extra field into the equation returns nothing.
The query below returns all attachments
where the lottery_year
is equal to $y
, and obviously $y
can be 2011
or earlier.
Now my next step I want to bring in another meta_field
called photo_technique
and give that one a value.
Question, how do I do that?
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE ($wpdb->postmeta.meta_key = 'lottery_year' AND $wpdb->postmeta.meta_value = $y)
AND $wpdb->posts.post_type = 'attachment'
ORDER BY $wpdb->postmeta.meta_value ASC
LIMIT 100
";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想显示包含
lottery_year = $y
(或)photo_technique = $x
的帖子,您可以使用:编辑
如果您想显示同时具有
lottery_year = $y
( AND )photo_technique = $x
的结果,您可以使用下列的查询:编辑
最后一个查询(AND 查询)在某些情况下会失败。例如你想要
lottery_year = $y AND photo_technique = $x
,但它也会返回 $x 和 $y 切换的结果,如下所示:lottery_year = $x AND photo_technique = $y
,这实际上取决于您何时可以依赖它,例如您是否有一种称为“2001”的 photo_technique(女巫也可以作为 lottery_year 传递) 。请小心,或者在 dba.stackexchange.com 或 wordpress.stackexchange.com 上提出这个问题。If you whant to display posts that have either
lottery_year = $y
( or )photo_technique = $x
, you could use :Edit
If you whant to display results that have both
lottery_year = $y
( AND )photo_technique = $x
, you could use the following query :Edit
The last query ( the AND one ) will fail in some circumstances. For example you whant
lottery_year = $y AND photo_technique = $x
, but it will allso return results that have the $x and $y switched like :lottery_year = $x AND photo_technique = $y
, it realy depends when you can rely on it or not, like do you have a photo_technique that is called for example '2001' ( witch could pass as a lottery_year too ) . Please be carefull with it or ask this question on dba.stackexchange.com or wordpress.stackexchange.com .