![](/img/trans.png)
[英]WP_Query Pagination not working with custom query on custom post type
[英]WP_Query to filter specific parent Custom Post Type?
我有兩種通過主題rt_book
和rt_chapter
創建的自定義帖子類型。 每個rt_book
都有多個rt_chapter
帖子。 當我使用single-rt_book.php
(這應該顯示特定的內容rt_book
后),我怎么可以過濾只有rt_chapter
屬於特定rt_book
加載?
我當前的WP_Query
如下(它返回所有rt_chapter
):
$args = array(
'post_type' => 'rt_chapter', // TODO: Filter specific book.
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC'
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
$posts[] = $query->post;
}
}
我通過函數add_post_meta({post ID of rt_book}, 'chapter_orders', {post ID of rt_chapter})
和update_post_meta()
將rt_book
的Post Meta設置為rt_chapter
帖子ID的數組來關聯這兩種自定義帖子類型。
試圖child_of
的選項WP_Query
,但不影響使用效果。
像這樣嘗試
$args = array(
'post_type' => 'rt_chapter',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'rt_book',
'value' => get_post_meta(get_the_ID(), 'chapter_orders', true),
'compare'=> 'IN'
),
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
$posts[] = $query->post;
}
}
嘗試這個。 希望這應該工作。
$tax_slug = get_query_var('taxonomy');
$args = array(
'post_type' => 'rt_chapter', // TODO: Filter specific book.
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'rt_book',
'field' => 'slug',
'terms' => $tax_slug
),
)
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.