在创建了一个名为ground_catalog
支持自定义字段,然后创建一些测试帖子。我添加了自定义字段is-active
(使用标准WordPress功能)并输入值true
对于一些帖子。
修复方法是将参数改为WP_Query
:
$result = new WP_Query( [
\'post_type\' => $post_type,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'meta_key\' => \'is-active\',
\'meta_value\' => \'true\', // Note that this is a STRING
] );
完整示例下面是完整的工作代码示例,其中包含一些可读性重构,以及当前过滤器项样式的修复和其他小更改:
add_filter( \'views_edit-ground_catalog\', \'wpse246143_add_admin_quick_link\' );
function wpse246143_add_admin_quick_link( $views ) {
$post_type = \'ground_catalog\';
// Bail immediately if we\'re not looking at the right post type.
if ( ! isset( $_GET[\'post_type\'] ) || $post_type !== $_GET[\'post_type\'] ) {
return $views;
}
// Handle the class applied to the link filter.
$link_class = false;
if ( isset( $_GET[\'active\'] ) && \'true\' === $_GET[\'active\'] ) {
$link_class = \'current\';
}
// Set up a query to determine how many posts are set up as is-active == true
$result = new WP_Query( [
\'post_type\' => $post_type,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'meta_key\' => \'is-active\',
\'meta_value\' => \'true\',
] );
// Generate the link for our filter.
// post_status=publish was removed to prevent a styling conflict with the existing \'Published\' filter.
$views[\'ground_catalog_active\'] = sprintf( \'<a href="%s" class="%s">%s<span class="count">(%d)</span></a>\',
admin_url( "edit.php?post_type={$post_type}&active=true" ),
$link_class,
__( \'Attivi\', \'text-domain\' ),
$result->found_posts
);
return $views;
}
add_action( \'init\', \'wpse246143_register_active\' );
function wpse246143_register_active() {
global $wp;
$wp->add_query_var( \'active\' );
}
add_action( \'parse_query\', \'wpse246143_map_active\' );
function wpse246143_map_active( $wp_query ) {
$meta_value = $wp_query->get( \'active\' );
if ( true == $meta_value ) {
$wp_query->set( \'meta_key\', \'is-active\' );
$wp_query->set( \'meta_value\', $meta_value );
}
}
为了完整性和故障排除,下面是注册
ground_catalog
post类型以及
is-active
元列。
// Register ground_catalog post type
add_action( \'init\', \'wpse246143_register_ground_catalog\' );
function wpse246143_register_ground_catalog() {
$labels = array(
"name" => __( \'Ground Catalogs\', \'your_text_domain\' ),
"singular_name" => __( \'Ground Catalog\', \'your_text_domain\' ),
);
$args = array(
"label" => __( \'Ground Catalogs\', \'your_text_domain\' ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => true,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "ground_catalog", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail", "custom-fields" ), );
register_post_type( "ground_catalog", $args );
}
// Add custom column to ground_catalog post list
add_filter( \'manage_ground_catalog_posts_columns\' , \'wpse246143_manage_ground_catalog_columns\' );
function wpse246143_manage_ground_catalog_columns( $columns ) {
return array_merge( $columns,
array( \'is-active\' => __( \'Is Active\', \'your_text_domain\' ) ) );
}
// Display custom column values on ground_catalog post list
add_action( \'manage_ground_catalog_posts_custom_column\' , \'wpse246143_ground_catalog_custom_column\', 10, 2 );
function wpse246143_ground_catalog_custom_column( $column, $post_id ) {
switch ( $column ) {
case \'is-active\':
$active = get_post_meta( $post_id, \'is-active\', true );
echo \'<strong>is-active: </strong>\';
var_dump( $active );
break;
}
}
演示我添加了一些
ground_post
用于故障排除和测试目的的条目。自定义列显示为
is-active
元值。将显示在
Active (Attivi)排序列是仅含污垢的地面和含树叶的地面,因为它们是唯一具有
is-active
元键设置值为
true
(字符串):
正在排序