簡體   English   中英

WordPress-通過過濾后的帖子獲取所有類別

[英]WordPress - Get all categories with filtered Posts

我正在使用wp_dropdown_categories生成類別列表作為下拉選項,通過將hide_if_empty參數設置為true ,該函數將只列出帶有附加帖子的類別。

但是我想實現的是我只想顯示具有活動帖子的類別 ,我有自定義字段from_dateto_date來確定帖子的有效性,因此我可以使用下面的meta_query過濾活動帖子:

array(
  'key'     => 'to_date',
  'value'   => $today_date,
  'compare' => '>='
)

有沒有一種方法可以通過元查詢過濾類別,因為當前只要有附加的帖子都將輸出類別。

試圖尋找類似的東西:

<?php 
  $args = array(
    'show_option_all' => 'All',
    'hide_empty'      => 1,
    'selected'        => $selected,
    'hide_if_empty'   => true,
    'value_field'     => 'slug',
    'meta_query'      => array(
      // Conditions to filter out categories without active posts
    ),
  ); 
?>

<div id="filter-select-wrapper">
  <?php wp_dropdown_categories( $args ); ?> 
</div>

使用以下代碼顯示從今天開始活躍的帖子:

<?php 
  $today = date( 'Y-m-d' );
  $args = array(
    'show_option_all' => 'All',
    'hide_empty'      => 1,
    'selected'        => $selected,
    'hide_if_empty'   => true,
    'value_field'     => 'slug',
    'meta_query' => array(
        array(
            'key' => 'to_date',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE'
        )
    )
  ); 
?>

<div id="filter-select-wrapper">
  <?php wp_dropdown_categories( $args ); ?> 
</div>

使用此代碼,

 <?php $args = array ( 'showposts' => '1', 'category_name' => 'apples', 'paged' => $paged ); $the_query = new WP_Query( $args ); if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 

有關更多信息,請參考鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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