根据用例场景,您可能需要编写一些自定义代码或一个小插件来实现这一点。如果您想过滤管理仪表板帖子列表中的帖子,那么可以使用以下截取的代码跳转开始
<?php
if (is_admin()){
//this hook will create a new filter on the admin area for the specified post type
add_action( \'restrict_manage_posts\', function(){
global $wpdb, $table_prefix;
$post_type = (isset($_GET[\'post_type\'])) ? quote_smart($_GET[\'post_type\'], true) : \'post\';
//only add filter to post type you want
if ($post_type == \'YOUR_POST_TYPE_HERE\'){
$values = array();
$query_years = $wpdb->get_results("SELECT year(post_date) as year from ".$table_prefix."posts
where post_type=\'".$post_type."\'
group by year(post_date)
order by post_date");
foreach ($query_years as &$data){
$values[$data->year] = $data->year;
}
?>
<select name="admin_filter_year">
<option value="">All years</option>
<?php
$current_v = isset($_GET[\'admin_filter_year\'])? $_GET[\'admin_filter_year\'] : \'\';
foreach ($values as $label => $value) {
printf(
\'<option value="%s"%s>%s</option>\',
$value,
$value == $current_v? \' selected="selected"\':\'\',
$label
);
}
?>
</select>
<?php
}
});
//this hook will alter the main query according to the user\'s selection of the custom filter we created above:
add_filter( \'parse_query\', function($query){
global $pagenow;
$post_type = (isset($_GET[\'post_type\'])) ? quote_smart($_GET[\'post_type\'], true) : \'post\';
if ($post_type == \'YOUR_POST_TYPE_HERE\' && $pagenow==\'edit.php\' && isset($_GET[\'admin_filter_year\']) && !empty($_GET[\'admin_filter_year\'])) {
$query->query_vars[\'year\'] = $_GET[\'admin_filter_year\'];
}
});
}
如果你想在前端做,那么我的建议是
custom query
和
ajax
. 对于这个场景,我不能提供一个示例,因为它有点复杂。由于您必须在前端使用elementor,因此您也可以为此开发一个小部件!