我无法通过WordPress管理员中的自定义字段进行过滤。我跟着this guide,但仍然无法让它发挥作用。
这是我在functions.php中所得到的:
function hospital_filter() {
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ('data-system' == $type) {
$values = array(
'True' => 1,
'False' => 0,
); ?>
<select name="hospital-invoiced">
<option value="">Hospital Invoiced</option>
<?php $current_v = $_GET['hospital-invoiced'] ? $_GET['hospital-invoiced'] : '';
foreach ($values as $label => $value) {
printf(
'<option value="%s"%s>%s</option>',
$value,
$value == $current_v ? ' selected="selected"' : '',
$label
);
} ?>
</select>
<?php }
}
add_action('restrict_manage_posts', 'hospital_filter');
function hospital_filter_query($query) {
global $pagenow;
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ('data-system' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['hospital-invoiced']) && $_GET['hospital-invoiced'] != '') {
$query->query_vars['meta_key'] = 'hospital_invoiced';
$query->query_vars['meta_value'] = $_GET['hospital-invoiced'];
}
}
add_filter('parse_query', 'hospital_filter_query');
当我将下拉菜单设置为&#34; True时,我已经达到成功过滤的程度。&#34;但是&#34;错误&#34;不起作用。此外,由于某种原因,下拉菜单默认为&#34; False&#34;当我第一次进入自定义帖子类型管理员时,尽管没有选择任何过滤选项。换句话说:
$value == $current_v ? ' selected="selected"' : '',
对于False选项,默认为true(希望有意义)。
任何帮助都将非常感激。谢谢!