在MULTSITE SWITCH_TO_BLOG()函数之后,是否可以使用税务查询来查询帖子?

时间:2021-05-26 作者:joshmoto

在我新安装的多站点上,安装了1个插件ACF。下面的所有代码都不会通过任何过滤器或操作在前端运行。

page.php 模板,我可以成功tax_query 直接从当前$site-purchase 博客

请参阅下面的代码和返回的转储get_posts()WP_Query.

// supplier args
$args = [
    \'numberposts\'   => -1,
    \'post_type\'     => \'purchase-supplier\',
    \'orderby\'       => \'title\',
    \'order\'         => \'ASC\',
    \'tax_query\'     => [
        [
            \'taxonomy\'  => \'supplier-type\',
            \'field\'     => \'slug\',
            \'terms\'     => \'data-packaging\'
        ]
    ]
];

// get supplier posts
$suppliers = get_posts($args);
// $suppliers = WP_Query($args);

dump($suppliers);
看见get_posts($args) 下面的结果,输出正确的预期结果,post\\u id 14(具有data-packaging 分配给它的术语)

Array
(
    [0] => WP_Post Object
        (
            [ID] => 14
            [post_author] => 1
            [post_date] => 2021-03-25 17:17:12
            [post_date_gmt] => 2021-03-25 17:17:12
            [post_content] =>
            [post_title] => Another company
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => another-company
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2021-05-26 20:31:25
            [post_modified_gmt] => 2021-05-26 20:31:25
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://localhost/purchase/?post_type=purchase-supplier&p=14
            [menu_order] => 0
            [post_type] => purchase-supplier
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

)
这里是new WP_Query($args) 输出

https://gist.github.com/joshmoto/18976d06ce375570fd14ad55d326c9dd


但是,如果我在上使用完全相同的代码$site_data 中的博客page.php 样板使用多站点switch_to_blog($id) 功能也要切换$site_purchase 要获取数据。。。

// switch to purchase blog
switch_to_blog($site_purchase);

// supplier args
$args = [
    \'numberposts\'   => -1,
    \'post_type\'     => \'purchase-supplier\',
    \'orderby\'       => \'title\',
    \'order\'         => \'ASC\',
    \'tax_query\'     => [
        [
            \'taxonomy\'  => \'supplier-type\',
            \'field\'     => \'slug\',
            \'terms\'     => \'data-packaging\'
        ]
    ]
];

// get supplier posts
$suppliers = get_posts($args);
// $suppliers = WP_Query($args);

dump($suppliers);

// switch back to data blog
switch_to_blog($site_data);
通过返回空数组get_posts($args)...

Array
(
)
这是new WP_Query($args) 返回的输出。。。

https://gist.github.com/joshmoto/42bb619ca289fcbe4ea4e969fe42a1b0


但是如果我修改上面在上使用的php代码段查询$site_data 中的博客page.php 样板只需删除tax_query 关键字值。。。

// switch to purchase blog
switch_to_blog($site_purchase);

// supplier args
$args = [
    \'numberposts\'   => -1,
    \'post_type\'     => \'purchase-supplier\',
    \'orderby\'       => \'title\',
    \'order\'         => \'ASC\',
//    \'tax_query\'     => [
//       [
//            \'taxonomy\'  => \'supplier-type\',
//            \'field\'     => \'slug\',
//            \'terms\'     => \'data-packaging\'
//        ]
//    ]
];

// get supplier posts
$suppliers = get_posts($args);
// $suppliers = WP_Query($args);

dump($suppliers);

// switch back to data blog
switch_to_blog($site_data);
这个get_posts($args) 返回预期结果。来自的所有帖子purchase-supplier 在数组中输出。。。

Array
(
    [0] => WP_Post Object
        (
            [ID] => 14
            [post_author] => 1
            [post_date] => 2021-03-25 17:17:12
            [post_date_gmt] => 2021-03-25 17:17:12
            [post_content] =>
            [post_title] => Another company
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => another-company
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2021-05-26 20:31:25
            [post_modified_gmt] => 2021-05-26 20:31:25
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://localhost/purchase/?post_type=purchase-supplier&p=14
            [menu_order] => 0
            [post_type] => purchase-supplier
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [1] => WP_Post Object
        (
            [ID] => 131
            [post_author] => 1
            [post_date] => 2021-05-20 21:15:56
            [post_date_gmt] => 2021-05-20 21:15:56
            [post_content] => 
            [post_title] => Barry Callebaut
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => barry-callebaut
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2021-05-25 23:12:05
            [post_modified_gmt] => 2021-05-25 23:12:05
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://localhost/purchase/?post_type=purchase-supplier&p=131
            [menu_order] => 0
            [post_type] => purchase-supplier
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [2] => WP_Post Object
        (
            [ID] => 12
            [post_author] => 1
            [post_date] => 2021-03-20 22:53:51
            [post_date_gmt] => 2021-03-20 22:53:51
            [post_content] => 
            [post_title] => Hancock Cash & Carry Ltd
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => hancock-cash-carry-ltd
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2021-05-26 14:54:08
            [post_modified_gmt] => 2021-05-26 14:54:08
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://localhost/purchase/?post_type=supplier&p=12
            [menu_order] => 0
            [post_type] => purchase-supplier
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [3] => WP_Post Object
        (
            [ID] => 13
            [post_author] => 1
            [post_date] => 2021-03-25 16:53:18
            [post_date_gmt] => 2021-03-25 16:53:18
            [post_content] => 
            [post_title] => Test
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => test
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2021-05-25 23:12:14
            [post_modified_gmt] => 2021-05-25 23:12:14
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://localhost/purchase/?post_type=purchase-supplier&p=13
            [menu_order] => 0
            [post_type] => purchase-supplier
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

)
这是new WP_Query($args) 返回的输出打开$site-data 没有tax_query...

https://gist.github.com/joshmoto/6df8c87f789564d64892b675c8a18a7f

<我就是想不通为什么tax_queryswitch_to_blog() 当所有其他测试显示tax_query 当前工作$site_purchase 博客和基本查询(无tax_query) 还可以很好地返回所有purchase-supplier 发布自$site_purchase 内的博客$site_data 博客

但只要我加上tax_query 至我的查询参数$site_data, 不purchase-supplier 返回帖子(id 14)。仅返回空数组。奇怪的

有什么想法吗?非常感谢。

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

是的,这是可能的,但您需要在这两个站点上注册分类法,因为switch_to_blog() 不为您注册分类法,它主要只切换数据库,但分类法不存储在数据库中,只存储分类法中的术语。

但您不必在页面加载时注册分类法。。(但这是正确的做法)。因为只要是注册的tax_query 将起作用。下面是一个注册supplier-type 分类法权限after 你打电话switch_to_blog():

// switch to purchase blog
switch_to_blog($site_purchase);

// Register the taxonomy.
$taxonomy = \'supplier-type\';
if ( ! taxonomy_exists( $taxonomy ) ) {
    register_taxonomy( $taxonomy, null, [] );
    // or call your custom function which registers the taxonomy using the proper args
}

// ... your code.
[编辑]我举了上述例子,因为(在评论中)你说,“我不知道你在说什么。”使用条件逻辑检查站点是否为站点,然后注册类型/税;以及税务只在购买网站上注册,而不是在数据网站上注册;。因此,即使您有条件注册了分类法,上述方法也应该有效。

此外,针对查询自定义帖子类型实际上可能不需要register_post_type 要被唤醒,但是tax_queries 可能实际上需要了解已注册的分类法;,是的,没错。

更具体地说,税务查询class 使用taxonomy_exists() 生成SQL语句时(JOINWHERE 子句)以获取税务查询的子句(请参见source on Trac), 如果指定的分类法未注册,则WHERE 条款将为0 = 1 (如中所示WHERE 1=1 AND ( 0 = 1 ) AND tsp_2_posts.post_type = \'purchase-supplier\' ...) 正如你在$suppliers->request 价值here. 因此,这就解释了空posts数组或为什么tax_queryswitch_to_blog() 功能;。

因此,请确保税务查询中使用的分类法已正确注册尽管如此WP_Query 不检查帖子类型是否存在(例如使用post_type_exists()) 在查询自定义帖子类型中的帖子时,如果切换到任何站点后,您需要获取帖子类型对象或数据(例如使用get_post_type_object()), 然后确保正确注册了post类型。