我一直在寻找你问题的相同答案,遇到了完全相同的问题,非常感谢你的深入研究。我在这里绝对是第一个提出这个补充的人!
尝试回答2)-我认为没有很好的理由排除它。我认为WordPress的分类术语与post和user实体相比,在功能上缺乏可悲之处(看看术语meta是如何在post和user meta之后出现的),我希望看到这种变化。
回答1)-我不知道这比您的解决方案好多少,但我提出的解决方法涉及通过get_terms_args
筛选,然后在客户端JS中手动插入选择下拉菜单+筛选按钮。下面是我的设置示例(修改为不使用我的特定类或常量),它通过某个术语元字段进行过滤:
PHP Logic
// @see https://developer.wordpress.org/reference/hooks/get_terms_args/
add_filter( \'get_terms_args\', \'taxonomy_filter\', 10, 2 );
function taxonomy_filter( $args, $taxonomies ) {
global $pagenow;
if ( \'edit-tags.php\' !== $pagenow || ! in_array( \'taxonomy-name\', $taxonomies, true ) ) { return $args; }
// Sort by most recently added terms, instead of alphabetically
$args[\'orderby\'] = \'term_id\';
$args[\'order\'] = \'desc\';
// Filter by term meta
$meta_key = ( isset( $_GET[\'meta_key\'] ) ) ? sanitize_text_field( $_GET[\'meta_key\'] ) : null;
$meta_value = ( isset( $_GET[\'meta_value\'] ) ) ? sanitize_text_field( $_GET[\'meta_value\'] ) : null;
if ( \'meta-filter\' === $meta_key && $meta_value ) {
$args[\'meta_key\'] = $meta_key;
$args[\'meta_value\'] = $meta_value;
}
// Note: for more complex filtering, use the $args[\'meta_query\'] array.
return $args;
}
此时,您只需转到(例如),就可以看到这一点
yoursite.com/wp-admin/edit-tags.php?taxonomy=taxonomy-name&meta_key=meta-filter&meta_value=foobar
.
Client-side jQuery logic
(请确保将此文件包括在
wp_enqueue_script()
, 但只有当
$pagenow === \'edit-tags.php\'
).
/**
* Param helpers
*/
// Get params in a JS object
var params = {};
window.location.search.substr(1).split( \'&\' ).forEach(function(item) {
params[ item.split( \'=\' )[0] ] = item.split( \'=\' )[1];
});
// Return param string based on the params object
function setParams() {
var string = \'?\';
for ( key in params ) {
var value = params[ key ];
string += key + \'=\' + value + \'&\';
}
return string.slice(0, -1); // Remove trailing &
}
/**
* Add dropdown filters + functionality to term tables
*/
if ( \'taxonomy-name\' === params.taxonomy ) {
// Create the dropdown menu & HTML
$( document.querySelector( \'.tablenav .bulkactions\' ) ).after( \'\\
<div class="alignleft actions"> \\
<select id="js-filter-dropdown"> \\
<option value="">Term Meta Filter</option> \\
<option value="foo">Foo</option> \\
<option value="bar">Bar</option> \\
<option value="baz">Baz</option> \\
</select> \\
<button id="js-filter" class="button" type="button">Filter</button> \\
</div> \\
\' );
// If we\'re already filtering the view, have the dropdown reflect that
var value = decodeURIComponent( params.meta_value ).replace(/\\+/g, \' \');
$( \'#js-filter-dropdown\' ).find( \'option[value="\' + value + \'"]\' ).prop( \'selected\', true );
// Set up the button action - see taxonomy_filter() for server-side filtering
$( \'#js-filter\' ).click(function() {
var value = $( \'#js-filter-dropdown\' ).val();
if ( value ) {
params.meta_key = \'meta-filter\';
params.meta_value = encodeURIComponent( value );
} else {
delete params.meta_key;
delete params.meta_value;
}
window.location.search = setParams();
});
}
Screenshot of the Filtering UI
Screenshot of the filtered table