Code snippet: Change the Page Length options in Posts Table Pro
Please note: This article is aimed at developers. We have provided an article on how to add code snippets. If you're not comfortable using them, then you can use our plugin customization service.
Posts Table Pro lets you set the number of posts per page and choose where the posts-per-page selector appears. The selector normally includes 10, 25, 50 and 100, together with your selected posts-per-page value if it is different. Some configurations also include an All option.
Use the following code snippet to change the choices or remove the All option. The snippet supports both the current frontend and the legacy jQuery DataTables frontend.
Code snippet
Add this PHP code to your theme or child theme, or with a code snippets plugin:
add_filter( 'posts_table_dataviews_config', 'barn2_ptp_change_page_length_options' );
function barn2_ptp_change_page_length_options( $config ) {
$config['rowsPerPageOptions'] = [ 10, 20, 30 ];
return $config;
}
add_filter( 'posts_table_datatables_config', 'barn2_ptp_change_legacy_page_length_options' );
function barn2_ptp_change_legacy_page_length_options( $config ) {
$page_lengths = [ 10, 20, 30 ];
$config['lengthMenu'] = [ $page_lengths, $page_lengths ];
return $config;
}
This example changes the dropdown to 10, 20 and 30 posts per page. It does not include the All option.
Make sure the table's Posts per page setting matches one of the values in the snippet, or add that value to both arrays.
Including the All option
If you want to keep the All option while changing the other choices, use this version instead:
add_filter( 'posts_table_dataviews_config', 'barn2_ptp_change_page_length_options' );
function barn2_ptp_change_page_length_options( $config ) {
$config['rowsPerPageOptions'] = [ 10, 20, 30, -1 ];
return $config;
}
add_filter( 'posts_table_datatables_config', 'barn2_ptp_change_legacy_page_length_options' );
function barn2_ptp_change_legacy_page_length_options( $config ) {
$config['lengthMenu'] = [
[ 10, 20, 30, -1 ],
[ 10, 20, 30, __( 'All', 'posts-table-pro' ) ],
];
return $config;
}
The value -1 tells Posts Table Pro to display every post in the table. Avoid offering All on a very large table because loading every row at once can affect performance.