WP查询分类输入与输出不同?

时间:2014-01-29 作者:Tim

我正在尝试构建自定义查询,需要包含当前类别ID。我以前的WP\\U查询参数为:

Array
(
    [post_status] => publish
    [posts_per_page] => -1
    [post_type] => courses
    [cat] => 70
当我在这个参数后面输出SQL结果时,使用$query->request查询;分类法读作95和203,而不是70?

SELECT wp_posts.* FROM wp_posts 
            INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
            INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 
            INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) 
            INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id) 
            INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id) WHERE 1=1 
            AND ( wp_term_relationships.term_taxonomy_id IN (95,203)
有谁能告诉我这是如何发生的/为什么发生的,以及我需要做什么才能从最初的70中找到95203,以便我也可以手动构建此查询?

非常感谢

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

一次奇妙的旅行“cat

假设我们有以下类别层次结构:

enter image description here

其中wp_term_taxonomy 表包括:

wp_term_taxonomy

我们想查询动物中的所有帖子category 其中id65:

$query = new WP_Query( array( \'cat\' => 65 ) );
并尝试理解为什么生成的SQL是:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID 
    FROM wp_posts  
    INNER JOIN wp_term_relationships 
        ON (wp_posts.ID = wp_term_relationships.object_id) 
    WHERE 1=1  
    AND ( wp_term_relationships.term_taxonomy_id IN (15,70, 75) ) 
        AND wp_posts.post_type = \'post\' 
    AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\') 
    GROUP BY wp_posts.ID 
    ORDER BY wp_posts.post_date 
    DESC LIMIT 0, 5
即为什么部分65 已更改为15, 70, 75.

Let\'s start:

在里面WP_Query() 我们的查询转换为:

$tax_query[0] = array( \'taxonomy\'           => \'category\',
                       \'terms\'              =>  array( 65 ),
                       \'field\'              => \'term_id\',
                       \'include_children\'   => 1
                   );
在给饥饿的人喂食之前:

 WP_Tax_Query( $tax_query )
它从$tax_query 大堆

在那里,它与默认值合并:

array( \'taxonomy\'          => \'\',
       \'terms\'             => array(),
       \'include_children\'  => true,
       \'field\'             => \'term_id\',
       \'operator\'          => \'IN\',
);
WP_Query() 对象希望返回生成的SQL查询,它调用WP_TAX_Query::get_sql() 方法

然后我们的阵列被“清理”和“变换”:

a) 它是cleaned 通过WP_TAX_Query::clean_query() 方法,导致:

array( \'taxonomy\'           => \'category\',
       \'terms\'              =>  array( 61, 13, 65 ),
       \'field\'              => \'term_id\',
       \'include_children\'   =>  1,
       \'operator\'           => \'IN\',
);
其中包含子类别get_term_children().

b) 它是transformed 通过WP_TAX_Query::transform_query() 方法,其中term_id 转换为相应的term_taxonomy_id 价值观

在我们的案例中,其结果是:

 SELECT term_taxonomy_id
 FROM wp_term_taxonomy
 WHERE taxonomy = \'category\'
 AND term_id IN (15, 61, 65)
15, 7075:

enter image description here

然后,我们的税务查询如下所示:

array( \'taxonomy\'           => \'category\',
       \'terms\'              =>  array( 15, 70, 75 ),
       \'field\'              => \'term_taxonomy_id\',
       \'include_children\'   =>  1,
       \'operator\'           => \'IN\',
);
在将其添加到的SQL查询部分之前WP_Query():

"join": INNER JOIN wp_term_relationships 
            ON (wp_posts.ID = wp_term_relationships.object_id)

"where": AND ( tfl_term_relationships.term_taxonomy_id IN (15,70,75) ) 
结束;-)

您还可以直接使用WP_TAX_Query 类,以调查生成的SQL。

例如:

    $tax_query = array();
    $tax_query[0] = array(
                   \'taxonomy\'           => \'category\',
                   \'terms\'              =>  array( 65 ),
                   \'field\'              => \'term_id\',
                   \'include_children\'   => 1
               );
    $t = new WP_TAX_Query( $tax_query );
    print_r( $t->get_sql( $GLOBALS[\'wpdb\']->posts, \'ID\' ) );
将给出以下输出:

Array
(
    [join] =>  INNER JOIN wp_term_relationships ON (tfl_posts.ID = wp_term_relationships.object_id)
    [where] =>  AND ( wp_term_relationships.term_taxonomy_id IN (15,70,75) )
)

结束

相关推荐

Copy post taxonomy?

我是Wordpress的新手,我想做的是:创建一个分类法(课程),该分类法与POST一样有效,但有区别:我需要为课程添加一些字段(如课程成本、年龄跨度、课程每周的哪一天等)。我想我可以在帖子分类中添加自定义字段,但我想将“普通帖子”与“课程”分开。我在PHP编程方面有很多经验,但Wordpress对我来说是新的。我了解分类法和挂钩的概念,并且了解WP如何处理主题、子主题、插件等。我在安装了一个名为Simple Taxonomy的插件https://github.com/herewithme/simple-