使用多语言术语的自定义WP查询

时间:2020-10-30 作者:jam

我在琢磨如何正确定义mysql查询。我为多段语言2和9定义了term\\u taxonomy\\u id id。我只想返回具有term\\u taxonomy\\u id 2(主要语言)的帖子,然后从结果中返回具有term\\u taxonomy\\u id 32,71,38的所有帖子。你能帮我一下吗?我在这个问题上错在哪里?

SELECT SQL_CALC_FOUND_ROWS
    agn_posts.ID,
    agn_posts.post_title,
    agn_term_relationships.term_taxonomy_id
FROM
    agn_posts
LEFT JOIN agn_term_relationships ON(
        agn_posts.ID = agn_term_relationships.object_id
    )
WHERE
    1 = 1 
    AND(
        agn_term_relationships.term_taxonomy_id IN(32) 
        OR agn_term_relationships.term_taxonomy_id IN(71)
        OR agn_term_relationships.term_taxonomy_id IN(38)
        )
    AND agn_term_relationships.term_taxonomy_id IN(2)
        
    AND agn_posts.post_type = \'property\' AND(
        agn_posts.post_status = \'publish\' OR agn_posts.post_status = \'private\'
    )
GROUP BY
    agn_posts.ID
ORDER BY
    RAND()
LIMIT 0, 50

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

如果条款2, 32, 7138 属于相同的分类法,如language, 然后看看我原来的答案。

否则WP_Query 希望如此:

$query = new WP_Query( [
    \'post_type\' => \'property\',
    \'tax_query\' => [
        \'relation\' => \'AND\', // for clauses 1 and 2
        [ // clause 1
            \'taxonomy\' => \'language\',
            \'terms\'    => 2,
        ],
        [ // clause 2
            \'relation\' => \'OR\', // for sub-clauses 1, 2 and 3
            [ // sub-clause 1
                \'taxonomy\' => \'taxonomy-name\',
                \'terms\'    => 32,
            ],
            [ // sub-clause 2
                \'taxonomy\' => \'taxonomy-name-2\',
                \'terms\'    => 71,
            ],
            [ // sub-clause 3
                \'taxonomy\' => \'taxonomy-name-3\',
                \'terms\'    => 38,
            ],
        ],
    ],
] );
这应该给你正确的IN() AND ( IN() OR IN() OR IN() ) 查询,对于SQL命令,只需执行以下操作echo $query->request; 然后使用命令执行任何需要的操作。:)

并确保用正确的名称替换分类名称。

原始答案请注意,我没有测试your code on GitHub, 但你要做的是:

查询分配给术语的帖子2

然后是分配给术语的32, 71, 38

可以在WP_Query 通过tax_query 参数,如下所示:

$query = new WP_Query( [
    \'post_type\' => \'property\',
    \'tax_query\' => [
        \'relation\' => \'AND\', // AND is the default
        [
            \'taxonomy\' => \'language\',
            \'terms\'    => 2,
        ],
        [
            \'taxonomy\' => \'language\',
            \'terms\'    => [ 32, 71, 38 ],
        ],
    ],
] );
如果您从上面的查询中检查SQL命令,即。echo $query->request, 该命令有两个LEFT JOIN 条款:

LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)
以及WHERE 对于tax_query 看起来像:

wp_term_relationships.term_taxonomy_id IN (2)
AND
tt1.term_taxonomy_id IN (32,71,38)
因此,您的SQL命令应该是这样的-即使用两个(左/内/等)连接与第一个连接来查询术语中的帖子2, 第二个是条款中的职位32, 71 和/或38.

下面是一个基于SQL命令的示例,除了(如上所述),我使用了前缀wp_ 而不是agn_, 我还使用了表别名,如p 对于wp_posts:

(注意,在这个示例中,我故意不使用SQL_CALC_FOUND_ROWS)

SELECT p.ID, p.post_title, tr.term_taxonomy_id
FROM wp_posts p
  LEFT JOIN wp_term_relationships tr ON p.ID = tr.object_id
  LEFT JOIN wp_term_relationships tr2 ON p.ID = tr2.object_id
WHERE 1 = 1
  AND tr.term_taxonomy_id IN(2)
  AND tr2.term_taxonomy_id IN(32, 71, 38)
  AND p.post_type = \'property\'
  AND (p.post_status = \'publish\' OR p.post_status = \'private\')
GROUP BY p.ID
ORDER BY RAND() # note that this can get VERY slow on large data/tables
LIMIT 0, 50

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post