WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 6 days ago

#39406 new enhancement

Make callback_args filterable in WP_Terms_List_Table

Reported by: tifosi Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7
Component: Taxonomy Keywords: has-patch
Focuses: administration Cc:
PR Number:

Description

The callback_args variable is specific to the WP_Terms_List_Table class. It defines a set of core variables that are passed to the get_terms function, and thereby to the generated WP_Term_Query instance.

They can't be modified so restricting the default terms displayed to the paginated display of all terms for that post-type / taxonomy association.

The reason for this enhancement request is to easily allow the development of drill-down filtering for custom columns, which are added via the filters:

add_action( 'manage_edit-{$taxonomy}_columns', 'add_column' );
add_action( 'manage_{$taxonomy}_custom_column', 'add_column_content, 10, 3 );
            
// Add the meta data column sortable
add_filter( 'manage_edit-{$taxonomy}_sortable_columns', 'add_column_sortable' ] );

It is possible to do this at a lower level in get_terms - from 4.6 at least - using the 'pre_get_terms action' and injecting / over-riding the WP_Meta_Query query_varsmeta_query? value e.g. with:

add_action( 'pre_get_terms', 'get_terms_meta' );
function get_terms_meta($query) {
    // store current query vars
    $query_vars = $query->query_vars;
    $args = [];    

    $args['meta_query'] = [
       [
        'key'       => 'meta_key',
        'value'     => 'meta_value',
        'compare'   => 'LIKE'
       ]
    ]; 

   if ( !empty( $args ) ) {
      $query->query_vars = array_merge( $query_vars, $args );    
   }
}

That requires a deep understanding of core funtions - as well as some core digging! It would be much more flexible if there was an entry point filter that could be used to modify the get_terms args to manipulate by custom field, e.g.

add_filter( 'manage_edit-{$taxonomy}_column_filter', 'column_filter' );

function column_filter( $args ) {

// conditions met e.g. $_GET['meta_key']
$args['meta_query'] = [
    [
        'key'       => 'meta_key',
        'value'     => 'meta_value',
        'compare'   => 'LIKE'
    ]
]; 

return $args;

The column field value could be modified to add the query arg for the meta_key via the column filter detailed earlier.

I've hacked it in a test version of the class and it's pretty functional.

$args = array( 
    'search' => $search, 
    'page' => $this->get_pagenum(), 
    'number' => $tags_per_page, 
);

to

$args = array( 
    'search' => $search, 
    'page' => $this->get_pagenum(), 
    'number' => $tags_per_page, 
);
$args = apply_filters( 'manage_edit-' . $this->screen->taxonomy .'_column_filter, $args );

Thanks

Attachments (1)

39406.diff (1.2 KB) - added by pbiron 6 days ago.

Download all attachments as: .zip

Change History (2)

@pbiron
6 days ago

#1 @pbiron
6 days ago

  • Focuses administration added
  • Keywords has-patch added

39406.diff adds a filter to filter the arguments before the call the get_terms() in WP_Terms_List_Table::display_rows_or_placeholder().

The name of the new filter is different that proposed by @tifosi, but is more inline with similar filters in other list tables (e.g., users_list_table_query_args and https://developer.wordpress.org/reference/hooks/ms_sites_list_table_query_args/ ms_sites_list_table_query_args]).

Note: See TracTickets for help on using tickets.