Created
December 28, 2016 13:24
-
-
Save ontiuk/4ad017640a0845ceb0aaf57c33946a70 to your computer and use it in GitHub Desktop.
WordPress: Taxonomy Term Meta Drill Down With Pre_Get_Terms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'pre_get_terms', 'filter_pre_get_terms', 10, 2 ); | |
/** | |
* Filter WP_Term_Query meta query | |
* - a better drill down for filtering term meta data | |
* - keeps pagination in line with term count | |
* - used in term edit page | |
* | |
* @param object $query WP_Term_Query | |
*/ | |
function filter_pre_get_terms( $query ) { | |
global $pagenow; | |
$taxonomy = filter_input( INPUT_GET, 'taxonomy', FILTER_SANITIZE_STRING ); | |
// only if current post type or edit page in admin | |
if ( !is_admin() || $pagenow !== 'edit-tags.php' || !in_array( $taxonomy, [ 'taxonomy' ] ) ) { return $query; } | |
// store current query vars | |
$query_vars = $query->query_vars; | |
$args = []; | |
// test meta | |
$meta = ( isset( $_GET['meta-field-name'] ) ) ? sanitize_text_field( urldecode( $_GET['meta-field-name'] ) ) : ''; | |
if ( empty( $meta ) ) { return; } | |
// set meta query | |
$args['meta_query'] = [ | |
[ | |
'key' => 'meta-field-name', | |
'value' => $meta, | |
'compare' => 'LIKE' | |
] | |
]; | |
// refresh query vars | |
$query->query_vars = array_merge( $query_vars, $args ); | |
} | |
// Requires term meta column filter to be active and column value to be wrapped in link with '&meta-field-name=meta-field-value' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment