【问题标题】:Wordpress custom query: cross-reference custom taxonomy from one post type with custom meta from anotherWordpress 自定义查询:来自一种帖子类型的交叉引用自定义分类与来自另一种类型的自定义元
【发布时间】:2012-10-19 18:08:05
【问题描述】:

我在使用自定义 Wordpress MySQL 查询时遇到问题。我的情况是这样的:我有用于帖子的常规帖子类型“帖子”和用于有关作者信息的自定义帖子类型“作者”。 “作者”帖子类型包含所有作者,但并非所有人都需要是帖子的作者。

每个帖子(类型为“帖子”和“作者”)都有一个带有作者确切姓名的自定义分类法(例如“John Smith”)。帖子类型“作者”为作者的名字和姓氏提供了额外的自定义元值(因为它们可能变得复杂并且更容易按姓氏、名字排序)。

现在我正在尝试选择所有已发布帖子的作者,计算他们的帖子数量并显示与他们的姓名相关联的元值。我不确定如何交叉引用来自一种帖子类型(“帖子”)的分类法与在一行中形成另一个(“作者”)的元值。

我想要什么:

John Smith    John    Smith    10

到目前为止我得到了什么:

John Smith    10    John
John Smith    10    Smith

我不知道下面的查询。任何帮助将不胜感激!

SELECT
    N, C, meta_value
    FROM
        (SELECT
            t.name AS N, count(*) AS C
                FROM
                    wp_2_posts p
                    INNER JOIN wp_2_term_relationships AS tr ON p.ID=tr.object_id
                    INNER JOIN wp_2_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
                    INNER JOIN wp_2_terms AS t ON tt.term_id = t.term_id
                WHERE 1=1
                    AND tt.taxonomy = 'myauthor'
                    AND p.post_type = 'post'
                    AND p.post_status = 'publish'
                GROUP BY
                    N
                ORDER BY
                C DESC) AS x
        INNER JOIN wp_2_posts p2
        INNER JOIN wp_2_term_relationships AS tr2 ON p2.ID=tr2.object_id
        INNER JOIN wp_2_term_taxonomy AS tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
        INNER JOIN wp_2_terms AS t2 ON tt2.term_id = t2.term_id
        INNER JOIN wp_2_postmeta AS m2 ON m2.post_id = p2.ID
            WHERE 1=1
                AND post_type = 'author'
                AND t2.name = x.N
                AND (m2.meta_key = 'lastname' OR m2.meta_key = 'firstname')

【问题讨论】:

    标签: mysql wordpress


    【解决方案1】:

    这是一个相当难看的解决方案,有什么改进的方法吗?

    SELECT
        N, C, m2.meta_value AS L, m3.meta_value AS F
        FROM
            (SELECT
                t.name AS N, count(*) AS C
                    FROM
                        wp_2_posts p
                        INNER JOIN wp_2_term_relationships AS tr ON p.ID=tr.object_id
                        INNER JOIN wp_2_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
                        INNER JOIN wp_2_terms AS t ON tt.term_id = t.term_id
                    WHERE 1=1
                        AND tt.taxonomy = 'myauthor'
                        AND p.post_type = 'post'
                        AND p.post_status = 'publish'
                    GROUP BY
                        N) AS x
            INNER JOIN wp_2_posts p2
            INNER JOIN wp_2_term_relationships AS tr2 ON p2.ID=tr2.object_id
            INNER JOIN wp_2_term_taxonomy AS tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
            INNER JOIN wp_2_terms AS t2 ON tt2.term_id = t2.term_id
            INNER JOIN wp_2_postmeta AS m2 ON m2.post_id = p2.ID
            INNER JOIN wp_2_posts p3
            INNER JOIN wp_2_term_relationships AS tr3 ON p3.ID=tr3.object_id
            INNER JOIN wp_2_term_taxonomy AS tt3 ON tr3.term_taxonomy_id = tt3.term_taxonomy_id
            INNER JOIN wp_2_terms AS t3 ON tt3.term_id = t3.term_id
            INNER JOIN wp_2_postmeta AS m3 ON m3.post_id = p3.ID
                WHERE 1=1
                    AND p3.post_type = 'author'
                    AND t3.name = x.N
                    AND m3.meta_key = 'firstname'
                    AND p2.post_type = 'author'
                    AND t2.name = x.N
                    AND m2.meta_key = 'lastname'
        ORDER BY
            C DESC,
            L
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多