admin管理员组文章数量:1420086
I have this code
while( $animes->have_posts() ) {
$animes->the_post();
$i++;
$animeID[$i] = $post->ID;
$args = array(
'orderby' => 'meta_value_num',
'order' => 'DESC',
'fields' => 'all',
'meta_query' => [['key' => 'episode_number','type' => 'NUMERIC',]]
);
$episodes[$i] = wp_get_post_terms(intval( $animeID[$i] ), 'episodes', $args );
}
}
I want to merge all $episodes[$i] in one array is it possible?
I have this code
while( $animes->have_posts() ) {
$animes->the_post();
$i++;
$animeID[$i] = $post->ID;
$args = array(
'orderby' => 'meta_value_num',
'order' => 'DESC',
'fields' => 'all',
'meta_query' => [['key' => 'episode_number','type' => 'NUMERIC',]]
);
$episodes[$i] = wp_get_post_terms(intval( $animeID[$i] ), 'episodes', $args );
}
}
I want to merge all $episodes[$i] in one array is it possible?
Share Improve this question asked Jul 11, 2019 at 18:45 Gamal MohamedGamal Mohamed 11 Answer
Reset to default 0Rather than placing the result of wp_get_post_terms()
into $episodes[$i]
, you can just merge it into the $episodes
array using array_merge()
, like this:
$episodes = []; // Initialise as an empty array first.
while( $animes->have_posts() ) {
$animes->the_post();
$animeID[] = get_the_ID();
$args = array(
'orderby' => 'meta_value_num',
'order' => 'DESC',
'fields' => 'all',
'meta_query' => [
[
'key' => 'episode_number',
'type' => 'NUMERIC',
],
],
);
$episodes = array_merge( $episodes, wp_get_post_terms( get_the_ID(), 'episodes', $args );
}
I also simplified the code, removing the unnecessary $i
variable and using get_the_ID()
to get the current post ID.
All that being said, if you have a query of posts, and want to get the terms used by the posts in that query, you can just past a list of post IDs to get_terms()
using the object_ids
argument:
$anime_ids = wp_list_pluck( $animes->posts, 'ID' );
$episodes = get_terms(
'object_ids' => $anime_ids,
'meta_key' => 'episode_number',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
本文标签: phpMerge wpgetpostterms
版权声明:本文标题:php - Merge wp_get_post_terms 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745328072a2653683.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论