親の分類法を取得する方法は?
-
16-10-2019 - |
質問
- 「アーティスト」と「コンサート」という2つの税関の投稿タイプがあります。
- 「コンサート」カスタムポストタイプは、「アーティスト」カスタムポストタイプの子です
- 「アーティスト」カスタムポストタイプには、「ジャンル」の分類法があります。
私がやろうとしていること(たとえば):「ポップ」ジャンルのアーティストに属するすべてのコンサートをリストします。
これが私の夢の質問です:
SELECT * FROM posts WHERE post_type = "concert" AND post_parent_term = "pop"
現在、そのようなものはないと思います post_parent_term, 、私が間違っていることを願っています...(「コンサート」のカスタムポストタイプとVoilàに「ジャンル」の分類法を追加できることを知っています!しかし、それを達成する別の方法があるかどうか知りたいです)。
Advanceに感謝します。
解決
私がやろうとしていること(たとえば):「ポップ」ジャンルのアーティストに属するすべてのコンサートをリストします。
2つのステップで行うことができます。
// 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_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;
}
}
所属していません wordpress.stackexchange