Currently the core/page-list block doesn't provide any filters for modifying the pages query. The user has to either use the get_pages_query_args or get_pages filters, should they want to filter the query. However, targeting the correct get_pages() call is difficult as the filters are not aware of in which context they are used - in a block or some other place.
The user can get creative and wrap the default block rendering callback in a helper function to include logic to try to determine the correct block, which query needs to be filtered. This is demonstrated in an answer written by me to the above WPSE question.
What is your proposed solution?
If the core/page-list block provided a query args filter, the query modification would be a lot easier and could be done in a more accurate and reliable way.
This could be done for example by extracting the pages query from the block's rendering callback into a helper function. This helper would then contain the query args filter and return an array of pages to be used in the rendering callback. The helper would receive the block attributes and the block instance as parameters to provide context for the filter.
This kind of filter would also mimic the widget_pages_args filter the WP_Widget_Pages uses.
The extracted query helper function,
/**
* Query pages for `core/page-list` block
*
* @param array $attributes The block attributes.
* @param WP_Block $block The parsed block.
*
* @return array Returns array of pages
*/
function block_core_page_list_pages_query( $attributes, $block ) {
/**
* Filters the arguments for the `core/page-list` block
*
* @see get_pages()
*
* @param array $args An array of arguments to query the pages array.
* @param array $attributes The block attributes.
* @param WP_Block $block The parsed block.
*/
$args = apply_filters(
'block_core_page_list_pages_query_args',
array(
'sort_column' => 'menu_order,post_title',
'order' => 'asc',
),
$attributes,
$block
);
$all_pages = get_pages( $args );
return is_array( $all_pages ) ? $all_pages : array();
}
The above helper function used in the rendering callback,
What problem does this address?
This ticket was inspired by the question "How do you add a filter to wp-block-page-list?" at WordPress StackExchange.
Currently the
core/page-list
block doesn't provide any filters for modifying the pages query. The user has to either use theget_pages_query_args
orget_pages
filters, should they want to filter the query. However, targeting the correctget_pages()
call is difficult as the filters are not aware of in which context they are used - in a block or some other place.The user can get creative and wrap the default block rendering callback in a helper function to include logic to try to determine the correct block, which query needs to be filtered. This is demonstrated in an answer written by me to the above WPSE question.
What is your proposed solution?
If the
core/page-list
block provided a query args filter, the query modification would be a lot easier and could be done in a more accurate and reliable way.This could be done for example by extracting the pages query from the block's rendering callback into a helper function. This helper would then contain the query args filter and return an array of pages to be used in the rendering callback. The helper would receive the block attributes and the block instance as parameters to provide context for the filter.
This kind of filter would also mimic the
widget_pages_args
filter theWP_Widget_Pages
uses.The extracted query helper function,
The above helper function used in the rendering callback,