以下代码获取与最近20篇“文章”或“观点”帖子相关的“组织”分类术语。
首先,它得到了帖子,$recent_posts
.其次,它推动了其中的每一项”organisation
“新阵列上的术语,$recent_companies
.
稍后,我将重复数据消除并显示这些术语。一切正常。
唯一的问题是,启用调试后,我可以看到foreach ($orgs_in_post as $org_single) {
引发以下PHP警告:
Warning: Invalid argument supplied for foreach()
它不会停止代码的工作。我刚刚发现了这个警告。但我想消除它。那么这段代码是怎么回事?
<?php
// Get latest posts
$args = array(
\'numberposts\' => 20,
\'offset\' => 0,
\'category\' => 0,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\',
\'include\' => \'\',
\'exclude\' => \'\',
\'meta_key\' => \'\',
\'meta_value\' =>\'\',
\'post_type\' => array(\'article\',\'viewpoint\'),
\'post_status\' => \'publish\',
\'suppress_filters\' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
$recent_companies = array();
// Get companies in latest posts
foreach ($recent_posts as $post) {
// get terms
$orgs_in_post = get_the_terms($post[\'ID\'], \'company\');
foreach ($orgs_in_post as $org_single) {
array_push($recent_companies, $org_single->term_id);
}
}