UPDATE: I figured it out! My fix was I could only filter by Columns which were specified to be sortable. Hopefully this helps someone else!
因此,对于我的自定义帖子类型,我只有一个列设置为可排序。我添加了我试图筛选的专栏,然后砰的一声!过滤成功!
// Make Custom Post Type Custom Columns Sortable
function cpt_custom_columns_sortable( $columns ) {
// Add our columns to $columns array
$columns[\'item_number\'] = \'item_number\';
$columns[\'coat_school\'] = \'coat_school\';
return $columns;
} add_filter( \'manage_edit-your-custom-post-type-slug_sortable_columns\', \'cpt_custom_columns_sortable\' );
因此,包括上述内容,我的整个代码解决方案如下所示:
/**
* Add School as a Filter Drop Menu
* Create the dropdown
* Change POST_TYPE to the name of your custom post type
*/
function ssp_school_custom_column_filter() {
// Only show the filter drop menu to Admins/SSP Users
$user = wp_get_current_user();
$role = ( array ) $user->roles;
if ( in_array("administrator", $role ) || in_array("ssp_user", $role ) ) {
// do nothing - this is backwards... shouldn\'t need to do it this way.
} else { return; }
$type = \'post\'; // Set default to be overridden
// Check for the Post Type
if (isset($_GET[\'post_type\'])) {
$type = $_GET[\'post_type\'];
}
$types = array( \'member\' );
$types_uniforms = ssp_get_uniform_items_names_by_size(\'all\');
$types = array_merge( $types , $types_uniforms );
// Only add filter to certain post types
if ( in_array( $type , $types) ) {
// Get all Schools
$schools_args = array(
\'post_type\' => \'schools\',
\'post_status\' => \'publish\',
\'posts_per_page\'=> \'-1\',
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'fields\' => \'ids\',
);
$schools = new WP_Query( $schools_args );
// Populate the List of Schools
if ( $schools->have_posts() ) {
$values = array();
while ( $schools->have_posts() ) {
$schools->the_post();
$values += [ get_the_title() => get_the_ID() ];
}
} else {
$values = array(
\'Error: No Schools Available\' => \'error\',
);
}
?>
<select name="ssp_school_custom_column_filter">
<option value="">Filter by School</option>
<?php
$current_school = isset($_GET[\'ssp_school_custom_column_filter\']) ? $_GET[\'ssp_school_custom_column_filter\'] : \'\';
foreach ($values as $label => $value) {
printf (
\'<option value="%s" %s>%s</option>\',
$value,
$value == $current_school ? \'selected="selected"\' : \'\',
$label
);
}
?>
</select>
<?php
}
} add_action( \'restrict_manage_posts\', \'ssp_school_custom_column_filter\' );
/**
* Handle Submitted School Filter
*
* Change META_KEY to the actual meta key
* and POST_TYPE to the name of your custom post type
*
* @param (wp_query object) $query
*
* @return Void
*/
function ssp_school_custom_column_posts_filter( $query ){
global $pagenow;
$type = \'post\';
if (isset($_GET[\'post_type\'])) {
$type = $_GET[\'post_type\'];
}
// Manually add members b/c they aren\'t Uniform Items
$types = array( \'member\' );
// Get all the Uniform Items and add them to Types
$types_uniforms = ssp_get_uniform_items_names_by_size(\'all\');
// Combine Members and Uniform Items
$types = array_merge( $types , $types_uniforms );
if ( in_array( $type , $types) &&
is_admin() &&
$pagenow == \'edit.php\' &&
isset($_GET[\'ssp_school_custom_column_filter\']) &&
$_GET[\'ssp_school_custom_column_filter\'] != \'\' &&
$query->is_main_query()
)
{
// Get the post type object
$type_obj = get_post_type_object( $type );
// The meta_key is dependent on the post type
$query->query_vars[\'meta_key\'] = \'assigned_school\';
$query->query_vars[\'meta_value\'] = intval($_GET[\'ssp_school_custom_column_filter\']);
$query->query_vars[\'meta_compare\'] = \'=\';
}
} add_filter( \'parse_query\', \'ssp_school_custom_column_posts_filter\' );
我也有同样的问题。
我发现的所有代码示例如下所示,我们的代码(你的和我的)似乎都与这些示例一样,但似乎都不起作用
Filter by custom field in custom post type on admin page