1. Knowledge Base
  2. Posts Table Pro
  3. Developer documentation

Hooks and filters

There are a number of filters provided in Posts Table Pro which allow you to customize the behavior of the plugin.

Please note that this code is aimed at developers. If you don’t know how to use this code then you should ask your developer. If you don’t have one, we recommend posting a job on Codeable. We have partnered with them to provide plugin customizations for our customers.

Need professional assistance?
We've partnered with Codeable to provide our customers with expert help if required.

Filters

Language filters

There are various filters provided for manipulating text strings and messages used in the plugin. Please refer to this article for filters to override the text strings.

posts_table_minimum_search_term_length

Set the minimum search term length allowed in the main search box. Defaults to 3 characters.

add_filter( 'posts_table_minimum_search_term_length', function( $length ) { 
    return 5; 
} );

posts_table_search_filter_get_terms_args

Filters the args passed to get_terms() when building the search filters.

add_filter( 'posts_table_search_filter_get_terms_args', function( $args, $taxonomy, Posts_Table_Args $table_args ) { 
   // Do something with args, e.g:
   $args['orderby'] = 'menu_order';
   return $args;
}, 10, 3 );

posts_table_search_filter_terms_<taxonomy>

Filters the terms to use in the search filter dropdown list. The filter name is dynamic – replace <taxonomy> with the relevant taxonomy slug.

add_filter( 'posts_table_search_filter_terms_category', function( $terms, $taxonomy,Posts_Table_Args $table_args ) { 
    // Do something with terms
    return $terms;
}, 10, 3 );

posts_table_data_filters

Filter all the search filters at once. Returns an array of arrays, with the column used as the top-level array key.

 add_filter( 'posts_table_data_filters', function( $filters, Posts_Table_Args $table_args ) { 
    // Do something with the filters, e.g:
    $filters['categories']['heading'] = 'Choose a category';
    return $filters;
}, 10, 2 );

posts_table_data_config

Filters which allows you to override the inline table configuration used when the DataTables script is initialised. You can use this filter, for example, to disable or enable certain table features. The following example disables the ordering/sorting feature in all product tables:

// Disable column sorting
add_filter( 'posts_table_data_config', function( $config, Posts_Table_Args $table_args) {
    $config['ordering'] = false;
    return $config;
}, 10, 2 );

posts_table_open_posts_in_new_tab

Product tables contain various links to the product details page. For example, the name column will (by default) include a link to the product page.

These will open in the same tab or window by default. To open them in a new tab use the posts_table_open_posts_in_new_tab filter. For example:

add_filter( 'posts_table_open_posts_in_new_tab', '__return_true' );

posts_table_process_shortcodes

By default, Posts Table Pro will strip shortcodes from the post content so any content that would appear inside these shortcodes will not appear in the table. You can enable shortcodes on a per-table basis using the shortcodes option, but if you want to enable them globally, return true from this filter as follows:

add_filter( 'posts_table_process_shortcodes', '__return_true' );

Whether to run the posts table shortcode on the search results page. Defaults to false. Normally, you would want to prevent the shortcode running within the search results, as only a snippet or excerpt of each page or post is shown in the search results. However, if you want to enable posts tables in the search results, set this filter to return true:

add_filter( 'posts_table_run_in_search', '__return_true' );

posts_table_data_<column>

There are various data filters available, which allow you to modify the data added to the table. One is provided for each column in the form posts_table_data_<column>.

For example, to filter the image column you would add a filter called posts_table_data_image. (For custom taxonomies and custom fields, please see the below).

Each filter takes 2 arguments – the data to be added to the table (e.g. the post image) and the current post object.

add_filter( 'posts_table_data_image', function( $image, $post ) {
    // do something with $image
    return $image;
} );

posts_table_data_custom_taxonomy_<taxonomy>

Filter which allows you to modify the value of the data in a custom taxonomy column. Accepts 2 arguments – the $terms as a comma-separated string, and the current post object. For example, if we want to override the data for taxonomy column tax:sector, we would use:

add_filter( 'posts_table_data_custom_taxonomy_sector', function( $terms, $post ) {
    // do something with $terms
    return $terms;
}, 10, 2 );

posts_table_data_custom_field

Filter which allows you to override the value of all custom fields in the table. It accepts 3 arguments – the custom field value, the custom field name and the current post object.

// Override the 'extra_notes' custom field
add_filter( 'posts_table_data_custom_field', function( $value, $field, $post ) {
    if ( 'extra_notes' === $field ) {
       // do something with $value
    }
    return $value;
}, 10, 3 );

posts_table_data_custom_field_<field>

Similar to the posts_table_data_custom_field filter, but specific to a custom field. The <field> portion of the filter is replaced by the field name. It accepts 2 arguments – the field value and the current post object.

// Override the 'extra_notes' custom field
add_filter( 'posts_table_data_custom_field_extra_notes', function( $value, $post ) {
    // do something with $value
    return $value;
}, 10, 2 );

Use this filter to override the URL used for a URL-based custom field. Note: this filter is only applied when the custom field value is a URL (i.e. begins with http:// or https://).

add_filter( 'posts_table_url_custom_field_link', function( $url, $field, $post ) { 
    return $link;
}, 10, 3 );

posts_table_url_custom_field_text

Use this filter to override the text used for a URL-based custom field. Defaults to the URL without the leading http://. Note: this filter is only applied when the custom field value is a URL (i.e. begins with http:// or https://).

add_filter( 'posts_table_url_custom_field_text', function( $text, $field, $post ) { 
   // do something with $text 
    return $text;
}, 10, 3 );

posts_table_custom_field_stored_date_format

If you are using date-based custom fields in your table, you may find that your dates are sometimes incorrectly sorted or formatted. The most likely cause of this is the date conversion functions used by the plugin which are built into PHP.

For example, dates such as “12/04/2018” are assumed to be in U.S. format with the month first. So this example date would be December 4th, 2018.

If the date is separated by dash (-), dot (.) or other symbol, it’s assumed to be a European/Australian date with the day before the month. If you’re using dates separated by a forward slash but in this format: d/m/Y, the plugin will not be able to sort these correctly, or format them if you’re using the date_format option. To fix this, you will need to set this filter.

Note: this is not related to how the custom field is displayed in the table – see the date_format option for controlling the output of date custom fields.

add_filter( 'posts_table_custom_field_stored_date_format', function( $format, $field ) { 
    if ( 'despatch_date' === $field ) {
        return 'd/m/Y';
    }
    return ''; 
}, 10, 2 );

posts_table_custom_field_is_eu_au_date

An alternative to the above filter, you can use this shorthand filter to quickly indicate your field is stored in European/Australian format (i.e. day before month).

The filter can be applied globally to all date-based custom fields, as follows:

​add_filter( 'posts_table_custom_field_is_eu_au_date', '__return_true' );

Or you can use it for individual fields only, like this:

​add_filter( 'posts_table_custom_field_is_eu_au_date', function( $is_eu_date, $field ) {
    if ( 'event_date' === $field ) {
        return true;
    }
    return false;
}, 10, 2 );

posts_table_acf_value

Filters which allows you to override the custom field values returned from Advanced Custom Fields. It accepts 3 arguments – the custom field value, the ACF field object and the current post ID.

add_filter( 'posts_table_acf_value', function( $value, $field_obj, $post_id ) {
    if ( 'file' === $field_obj['type'] ) {
       // do something with $value
    }
    return $value;
}, 10, 3 );

posts_table_more_content_text

Filter which controls the text/HTML appended when data in the content or excerpt columns is truncated. Defaults to  &hellip;.

add_filter( 'posts_table_more_content_text', function( $more_text ) {
    return ' [...]';
} );

posts_table_separator_<type>

Filter which allows you to change the separator used between categories, tags, terms and custom fields. For example, a post can have multiple categories, each separated by a comma. This filter allows you to change the comma for a different character, line breaks, or other custom HTML.

The filter must include the relevant type. Possible values for type are: categories, tags, terms, custom_field, and custom_field_row.

For example, to change the separator between categories in the table, add this filter:

add_filter( 'posts_table_separator_categories', function( $sep ) {
    return '<br/>';
} );

posts_table_custom_class

Use this filter to add additional classes to the posts table’s <table> tag. E.g.

add_filter( 'posts_table_custom_class', function( $class, $posts_table ) {
    return 'featured-table';
}, 10, 2 );

posts_table_row_class

Use this filter to add extra CSS classes to each row in the table, or selectively based on the current post.

add_filter( 'posts_table_row_class', function( $classes, $post ) {
    if ( 23 === $post->ID ) {
        $classes[] = 'featured';
    }
    return $classes;
}, 10, 2 );

posts_table_row_attributes

Use this filter to add extra attributes (e.g. data attributes) to the <tr> element in the table. Should return an array of key => value pairs.

add_filter( 'posts_table_row_attributes', function( $attributes, $post ) {
    $attributes['data-foo'] = 'bar';
    return $attributes;
}, 10, 2 );

posts_table_column_class_<column>

This filter allows you to add additional column classes to your table. Replace <column> with the column that you want to add a class to. You may add additional classes, but do not remove any from the list passed in, as it may affect the operation of the table.

add_filter( 'posts_table_column_class_title', function( $classes ) {
    $classes[] = 'highlight';
    return $classes;
} );

posts_table_column_heading_<column>

Filter which allows you to override a column heading for all posts tables on your site. If a custom heading is specified within the shortcode itself (in the columns option), then it will take priority over the value returned from this filter.

add_filter( 'posts_table_column_heading_title', function( $heading ) {
    return 'My Title';
} );

posts_table_column_priority_<column>

Filter which allows you to override a column priority for all posts tables on your site. If a priority is specified within the shortcode itself (using the priorities option), then it will take priority over the value returned from this filter.

add_filter( 'posts_table_column_priority_date', function( $priority ) {
    return 10;
} );

posts_table_column_width_<column>

Filter which allows you to override a column width for all posts tables on your site. If a width is specified within the shortcode itself (using the widths option), then it will take priority over the value returned from this filter.

add_filter( 'posts_table_column_width_author', function( $width ) {
    return '100px';
} );

posts_table_column_searchable_<column>

This filter allows you to set whether or not a column is “searchable” in the posts table. If a column is searchable, it means that the column will be used when the user types something into the search box above the table.

By default, the plugin includes all columns for searching, except for the image column (unless you’re using the lazy load option, in which case only the title and content columns are searchable). To override this behavior when you’re not using lazy load, use this filter with the appropriate column name added to the filter.

E.g. to exclude the title column from the table search:

add_filter( 'posts_table_column_searchable_title', '__return_false' );

If you wish to disable searching for a custom field or taxonomy, use the column name without the cf: or tax: prefix:

add_filter( 'posts_table_column_searchable_my_custom_field', '__return_false );

posts_table_column_sortable_<column>

This filter allows you to set whether or not a column is “sortable” in the posts table. If a column is sortable, the table can be re-ordered by that column using the up/down arrows that appear in the column heading.

If you’re using lazy load, the sorting is restricted to certain columns only. If you’re not using lazy load, then sorting is enabled for all columns except image.

E.g. to prevent the table being sorted by title:

add_filter( 'posts_table_column_sortable_title', '__return_false' );

See the note above about using this filter for custom field or taxonomy columns.

posts_table_max_posts_limit

Filter which allows you to override the maximum posts limit (500 posts) set in Posts Table Pro. Note that this option only applies when you are not using the lazy_load option.

add_filter( 'posts_table_max_posts_limit', function( $limit, $posts_table ) {
    return 1000;
}, 10, 2 );

posts_table_data_cache_expiry

Filter which allows you to set the expiry time (in seconds) for the table data cache. The default cache expiry time is 6 hours, and is configured via the plugin settings.

add_filter( 'posts_table_data_cache_expiry', function( $expiry, Posts_Table_Cache $cache) {
    return 3 * DAY_IN_SECONDS;
}, 10, 2 );

posts_table_use_data_cache

Filter whether to use data caching globally across all posts tables. Should return true or false.

posts_table_query_args

Filter which allows you to override the query args passed to WP_Query prior to retrieving posts from the database. See WP_Query documentation for details.

add_filter( 'posts_table_query_args', function( $args, $posts_table ) {
    // do something with $args
    return $args;
}, 10, 2 );

posts_table_optimize_table_query

Use this filter to disable the database query optimisations applied by Posts Table Pro. Use this if you need to directly access the post_content or post_excerpt database fields, even when the content and excerpt columns are not displayed in your posts table.

add_filter( 'posts_table_optimize_table_query', '__return_false' );

posts_table_shortcode_output

Allows you to modify the HTML returned by the [posts_table] shortcode. Takes two parameters: the HTML and the Posts_Data_Table instance.

add_filter( 'posts_table_shortcode_output', function( $html, $table ) {
    // do something with HTML
    return $html;
}, 10, 2 );

posts_table_load_frontend_scripts

Filter to disable the loading of the frontend scripts and styles used in Posts Table Pro. Only use this if you want to load the scripts yourself (e.g. on specific pages).

add_filter( 'posts_table_load_frontend_scripts', '__return_false' );

posts_table_use_fitvids

Filter to disable the use of FitVids script used in Posts Table Pro. FitVids is used to ensure videos are sized correctly at smaller screen sizes.

add_filter( 'posts_table_use_fitvids', '__return_false' );

Actions

posts_table_parse_args

Fired when the posts table arguments have been parsed and set. Takes one argument – the Posts_Table_Args instance.

posts_table_before_get_table

Fired once before the table is created, before the attributes and headings are added to the table. Takes one argument – the Posts_Data_Table instance.

posts_table_after_get_table

Fired once after each the table is fully created and (for standard loading) the data has been added. Takes one argument – the Posts_Data_Table instance.

posts_table_before_get_data

Fired once before the data is fetched and added to the table. Takes one argument – the Posts_Data_Table instance.

posts_table_after_get_data

Fired once after the data is added to the table. Takes one argument – the Posts_Data_Table instance.

Still need help?
If searching the knowledge base hasn't answered your question, please contact support.

Related Articles