WordPress-通过过滤后的帖子获取所有类别

时间:2018-10-30 06:48:28

标签: php wordpress

我正在使用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>

2 个答案:

答案 0 :(得分:-1)

使用以下代码显示从今天开始活跃的帖子:

<?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>

答案 1 :(得分:-1)

使用此代码,

<?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();
?>

有关更多信息,Reference Link

相关问题