[英]WordPress - Get all categories with filtered Posts
我正在使用wp_dropdown_categories
生成类别列表作为下拉选项,通过将hide_if_empty
参数设置为true
,该函数将只列出带有附加帖子的类别。
但是我想实现的是我只想显示具有活动帖子的类别 ,我有自定义字段from_date
和to_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.