-
16-10-2019 - |
题
- 我有2个海关帖子类型:“艺术家”和“音乐会”,
- “音乐会”自定义帖子类型是“艺术家”自定义帖子类型的孩子,
- “艺术家”自定义帖子类型具有“流派”分类学。
我要做的事情(例如):列出属于“流行”类型的艺术家的所有音乐会。
这是我梦想的疑问:
SELECT * FROM posts WHERE post_type = "concert" AND post_parent_term = "pop"
我认为目前没有这样的事情 post_parent_term, ,希望我错了...(我知道我可以在“音乐会”定制帖子类型和voilà中添加“流派”分类法!但是我很想知道是否还有另一种方法可以实现这一目标)。
谢谢。
解决方案
我要做的事情(例如):列出属于“流行”类型的艺术家的所有音乐会。
您可以通过两个步骤进行操作:
// 1. Get all the pop artist IDs
$artist_ids = get_posts( array(
'fields' => 'ids',
'post_type' => 'artist',
'genre' => 'pop'
) );
// 2. Get all the concerts associated to those artists
$artist_ids = implode( ',', array_map( 'absint', $artist_ids ) );
$concerts = $wpdb->get_results( "
SELECT * FROM $wpdb->posts
WHERE post_type = 'concert'
AND post_status = 'publish'
AND post_parent IN ({$artist_ids})
ORDER BY post_date DESC
" );
wp_query中有一个post_parent参数,但是 不接受数组, ,因此直接查询。
其他提示
父页面存储在$ post-> post_parent中
因此,您只需以这种方式获取父帖,然后询问其分类/类别/标签信息。
不确定它是否正确,但是您可以创建嵌套循环:
//首先让所有艺术家使用流行语
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'pop'
))
'post_type' => 'Artist',
'posts_per_page' => -1
);
$Artists = new WP_Query( $args );
//loop through them and get there child posts of concerts
if ( $Artists->have_posts() ) {
while ( $Artists->have_posts() ) {
$Artists->the_post();
$last_artist = $post;
$Concerts = new WP_Query();
$Concerts->query(array(
'post_type' => 'concert',
'posts_per_page' => -1,
'post_parent' => $post->ID
));
while ( $Concerts->have_posts() ) {
$Concerts->the_post();
//do concert stuff here
//the_title();
//the_content();
}
wp_reset_postdata();
$post = $last_artist;
}
}