wordpress query_posts 查看元信息

发布于 2024-11-02 02:37:19 字数 741 浏览 0 评论 0原文

我希望使用 query_posts 运行一个查询,让我查看 meta_fields。 我之前已经这样做过一次并达到了我想要的结果,但现在由于某种原因在方程中添加一个额外的字段不会返回任何结果。

下面的查询返回所有附件,其中lottery_year等于$y,显然$y可以是 2011 或更早版本。

现在我的下一步是引入另一个名为 photo_techniquemeta_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎夜雨 2024-11-09 02:37:19

如果您想显示包含 lottery_year = $yphoto_technique = $x 的帖子,您可以使用:

$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) OR ($wpdb->postmeta.meta_key = 'photo_technique' AND $wpdb->postmeta.meta_value = $x)
    AND $wpdb->posts.post_type = 'attachment'
    ORDER BY $wpdb->postmeta.meta_value ASC
    LIMIT 100
";

编辑

如果您想显示同时具有 lottery_year = $y ( AND ) photo_technique = $x 的结果,您可以使用下列的查询:

$querystr = "
    SELECT
        $wpdb->posts.*
    FROM 
        $wpdb->posts
    INNER JOIN $wpdb->postmeta ON
        $wpdb->posts.ID = $wpdb->postmeta.post_id 
        AND $wpdb->postmeta.meta_key IN ( 'lottery_year', 'photo_technique' )
        AND $wpdb->postmeta.meta_value IN ( '$y', '$x' )
    WHERE $wpdb->posts.post_type = 'attachment'
    GROUP BY $wpdb->posts.ID
    HAVING COUNT(1) = 2
    ORDER BY $wpdb->postmeta.meta_value ASC
    LIMIT 100
";

编辑

最后一个查询(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 :

$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) OR ($wpdb->postmeta.meta_key = 'photo_technique' AND $wpdb->postmeta.meta_value = $x)
    AND $wpdb->posts.post_type = 'attachment'
    ORDER BY $wpdb->postmeta.meta_value ASC
    LIMIT 100
";

Edit

If you whant to display results that have both lottery_year = $y ( AND ) photo_technique = $x, you could use the following query :

$querystr = "
    SELECT
        $wpdb->posts.*
    FROM 
        $wpdb->posts
    INNER JOIN $wpdb->postmeta ON
        $wpdb->posts.ID = $wpdb->postmeta.post_id 
        AND $wpdb->postmeta.meta_key IN ( 'lottery_year', 'photo_technique' )
        AND $wpdb->postmeta.meta_value IN ( '$y', '$x' )
    WHERE $wpdb->posts.post_type = 'attachment'
    GROUP BY $wpdb->posts.ID
    HAVING COUNT(1) = 2
    ORDER BY $wpdb->postmeta.meta_value ASC
    LIMIT 100
";

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文