wp-admin filter cpt by taxonomy 和 taxonomy 关系
wp-admin filter cpt by taxonomy and taxonomy relationship
我有类别和消息自定义 post 类型,它们都可以有一个名为子类别的自定义分类。
我希望能够通过自定义分类法过滤 wp-admin 上的消息,例如显示 Subcategory_1 中的所有消息。我设法用我找到的代码做到了 here.
问题是我还想按与消息具有相同子类别的类别过滤它们。
示例:
- Category_1 有 Subcategory_1
- Category_2 有 Subcategory_2
- Message_1 有 Subcategory_1
- Message_2 有 Subcategory_1
- Message_3 有 Subcategory_2
预期结果是,当用户从过滤器下拉列表中选择 Category_1 时,只有 Message_1 和 Message_2 显示。
当前代码(只能按子类别筛选):
function todo_restrict_manage_posts() {
global $typenow;
$args=array( 'public' => true, '_builtin' => false );
$post_types = get_post_types($args);
if ( in_array($typenow, $post_types) ) {
$filters = get_object_taxonomies($typenow);
foreach ($filters as $tax_slug) {
$tax_obj = get_taxonomy($tax_slug);
wp_dropdown_categories(array(
'show_option_all' => __('Show All '.$tax_obj->label ),
'taxonomy' => $tax_slug,
'name' => $tax_obj->name,
'orderby' => 'term_order',
'selected' => $_GET[$tax_obj->query_var],
'hierarchical' => $tax_obj->hierarchical,
'show_count' => true,
'hide_empty' => false
));
}
}
}
function todo_convert_restrict($query) {
global $pagenow;
global $typenow;
if ($pagenow=='edit.php') {
$filters = get_object_taxonomies($typenow);
foreach ($filters as $tax_slug) {
$var = &$query->query_vars[$tax_slug];
if ( isset($var) ) {
$term = get_term_by('id',$var,$tax_slug);
$var = $term->slug;
}
}
}
return $query;
}
add_action( 'restrict_manage_posts', 'todo_restrict_manage_posts' );
add_filter('parse_query','todo_convert_restrict');
当前过滤器(缺少按类别过滤)
问题出在这一行:
$var = &$query->query_vars[$tax_slug];
顺便说一句,& 符号不是必需的,$var 现在是术语的别名而不是 ID
所以这一行:
$term = get_term_by('id',$var,$tax_slug);
需要:
$term = get_term_by('slug',$var,$tax_slug);
如果有效请告诉我。
如果我理解正确的话,假设类别 post 类型 slug 是 categories
并且子类别 taxonomy slug 是 subcategories
,以下应该适合你:
SNIPPET 1:这将添加 "filter by Category" 下拉菜单:
add_action( 'restrict_manage_posts', 'add_categories_cpt_filter' );
function add_categories_cpt_filter( $post_type ) {
// Display the filter only on the Messages edit page.
if ( 'messages' === $post_type ) {
$posts = get_posts( [
'post_type' => 'categories',
'posts_per_page' => 99999,
'orderby' => 'title',
'order' => 'ASC',
] );
echo '<select name="categories_cpt" class="postform">';
if ( ! empty( $posts ) ) {
echo '<option value="0">Show All Categories</option>';
} else {
echo '<option value="0">No Categories</option>';
}
$selected = isset( $_GET['categories_cpt'] ) ?
absint( $_GET['categories_cpt'] ) : 0;
foreach ( $posts as $p ) {
printf( '<option value="%s"%s>%s</option>',
$p->ID, selected( $selected, $p->ID, false ),
esc_html( $p->post_title ) );
}
echo '</select>';
}
}
SNIPPET 2:这将按所选类别的条款过滤消息:
add_action( 'pre_get_posts', 'filter_messages_by_categories_cpt' );
function filter_messages_by_categories_cpt( $query ) {
// Enable the filter only on the Messages edit page.
if ( $query->is_admin && $query->is_main_query() &&
( $screen = get_current_screen() ) &&
( 'edit-messages' === $screen->id ) &&
! empty( $_GET['categories_cpt'] )
) {
$tax_query = $query->get( 'tax_query' );
$tax_query = is_array( $tax_query ) ? $tax_query : [];
$term_ids = get_terms( [
'taxonomy' => 'subcategories',
'object_ids' => absint( $_GET['categories_cpt'] ),
'fields' => 'ids',
] );
if ( ! is_wp_error( $term_ids ) && ! empty( $term_ids ) ) {
$tax_query[] = [
'taxonomy' => 'subcategories',
'terms' => $term_ids,
];
$query->set( 'tax_query', $tax_query );
}
}
}
您可以在有问题的代码之后添加上面的两个片段,即在 add_filter('parse_query','todo_convert_restrict');
.
之后
我有类别和消息自定义 post 类型,它们都可以有一个名为子类别的自定义分类。
我希望能够通过自定义分类法过滤 wp-admin 上的消息,例如显示 Subcategory_1 中的所有消息。我设法用我找到的代码做到了 here.
问题是我还想按与消息具有相同子类别的类别过滤它们。
示例:
- Category_1 有 Subcategory_1
- Category_2 有 Subcategory_2
- Message_1 有 Subcategory_1
- Message_2 有 Subcategory_1
- Message_3 有 Subcategory_2
预期结果是,当用户从过滤器下拉列表中选择 Category_1 时,只有 Message_1 和 Message_2 显示。
当前代码(只能按子类别筛选):
function todo_restrict_manage_posts() {
global $typenow;
$args=array( 'public' => true, '_builtin' => false );
$post_types = get_post_types($args);
if ( in_array($typenow, $post_types) ) {
$filters = get_object_taxonomies($typenow);
foreach ($filters as $tax_slug) {
$tax_obj = get_taxonomy($tax_slug);
wp_dropdown_categories(array(
'show_option_all' => __('Show All '.$tax_obj->label ),
'taxonomy' => $tax_slug,
'name' => $tax_obj->name,
'orderby' => 'term_order',
'selected' => $_GET[$tax_obj->query_var],
'hierarchical' => $tax_obj->hierarchical,
'show_count' => true,
'hide_empty' => false
));
}
}
}
function todo_convert_restrict($query) {
global $pagenow;
global $typenow;
if ($pagenow=='edit.php') {
$filters = get_object_taxonomies($typenow);
foreach ($filters as $tax_slug) {
$var = &$query->query_vars[$tax_slug];
if ( isset($var) ) {
$term = get_term_by('id',$var,$tax_slug);
$var = $term->slug;
}
}
}
return $query;
}
add_action( 'restrict_manage_posts', 'todo_restrict_manage_posts' );
add_filter('parse_query','todo_convert_restrict');
当前过滤器(缺少按类别过滤)
问题出在这一行:
$var = &$query->query_vars[$tax_slug];
顺便说一句,& 符号不是必需的,$var 现在是术语的别名而不是 ID
所以这一行:
$term = get_term_by('id',$var,$tax_slug);
需要:
$term = get_term_by('slug',$var,$tax_slug);
如果有效请告诉我。
如果我理解正确的话,假设类别 post 类型 slug 是 categories
并且子类别 taxonomy slug 是 subcategories
,以下应该适合你:
SNIPPET 1:这将添加 "filter by Category" 下拉菜单:
add_action( 'restrict_manage_posts', 'add_categories_cpt_filter' );
function add_categories_cpt_filter( $post_type ) {
// Display the filter only on the Messages edit page.
if ( 'messages' === $post_type ) {
$posts = get_posts( [
'post_type' => 'categories',
'posts_per_page' => 99999,
'orderby' => 'title',
'order' => 'ASC',
] );
echo '<select name="categories_cpt" class="postform">';
if ( ! empty( $posts ) ) {
echo '<option value="0">Show All Categories</option>';
} else {
echo '<option value="0">No Categories</option>';
}
$selected = isset( $_GET['categories_cpt'] ) ?
absint( $_GET['categories_cpt'] ) : 0;
foreach ( $posts as $p ) {
printf( '<option value="%s"%s>%s</option>',
$p->ID, selected( $selected, $p->ID, false ),
esc_html( $p->post_title ) );
}
echo '</select>';
}
}
SNIPPET 2:这将按所选类别的条款过滤消息:
add_action( 'pre_get_posts', 'filter_messages_by_categories_cpt' );
function filter_messages_by_categories_cpt( $query ) {
// Enable the filter only on the Messages edit page.
if ( $query->is_admin && $query->is_main_query() &&
( $screen = get_current_screen() ) &&
( 'edit-messages' === $screen->id ) &&
! empty( $_GET['categories_cpt'] )
) {
$tax_query = $query->get( 'tax_query' );
$tax_query = is_array( $tax_query ) ? $tax_query : [];
$term_ids = get_terms( [
'taxonomy' => 'subcategories',
'object_ids' => absint( $_GET['categories_cpt'] ),
'fields' => 'ids',
] );
if ( ! is_wp_error( $term_ids ) && ! empty( $term_ids ) ) {
$tax_query[] = [
'taxonomy' => 'subcategories',
'terms' => $term_ids,
];
$query->set( 'tax_query', $tax_query );
}
}
}
您可以在有问题的代码之后添加上面的两个片段,即在 add_filter('parse_query','todo_convert_restrict');
.