在我新安装的多站点上,安装了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_query
在
switch_to_blog()
当所有其他测试显示
tax_query
当前工作
$site_purchase
博客和基本查询(无
tax_query
) 还可以很好地返回所有
purchase-supplier
发布自
$site_purchase
内的博客
$site_data
博客
但只要我加上tax_query
至我的查询参数$site_data
, 不purchase-supplier
返回帖子(id 14)。仅返回空数组。奇怪的
有什么想法吗?非常感谢。