簡體   English   中英

WP_Query過濾特定的父自定義帖子類型?

[英]WP_Query to filter specific parent Custom Post Type?

我有兩種通過主題rt_bookrt_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM