Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sorting by name or count in categories block #66453

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Display a list of all terms of a given taxonomy. ([Source](https://github.com/Wo
- **Name:** core/categories
- **Category:** widgets
- **Supports:** align, interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** displayAsDropdown, label, showEmpty, showHierarchy, showLabel, showOnlyTopLevel, showPostCounts, taxonomy
- **Attributes:** displayAsDropdown, label, order, orderBy, showEmpty, showHierarchy, showLabel, showOnlyTopLevel, showPostCounts, taxonomy

## Code

Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/categories/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
"showLabel": {
"type": "boolean",
"default": true
},
"order": {
"type": "string",
"default": "asc"
},
"orderBy": {
"type": "string",
"default": "name"
Comment on lines +42 to +49
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the default query to prevent unexpected results.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also wondering if we should create a query attribute like the query loop has and include properties there like taxonomy. But I am not sure how it would work with backward compatibility.

}
},
"usesContext": [ "enhancedPagination" ],
Expand Down
47 changes: 46 additions & 1 deletion packages/block-library/src/categories/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export default function CategoriesEdit( {
label,
showLabel,
taxonomy: taxonomySlug,
order,
orderBy,
},
setAttributes,
className,
Expand All @@ -53,7 +55,13 @@ export default function CategoriesEdit( {
const isHierarchicalTaxonomy =
! isResolvingTaxonomies && taxonomy?.hierarchical;

const query = { per_page: -1, hide_empty: ! showEmpty, context: 'view' };
const query = {
per_page: -1,
hide_empty: ! showEmpty,
context: 'view',
order,
orderby: orderBy,
};
if ( isHierarchicalTaxonomy && showOnlyTopLevel ) {
query.parent = 0;
}
Expand Down Expand Up @@ -181,6 +189,29 @@ export default function CategoriesEdit( {
className: classes,
} );

const orderOptions = [
{
/* translators: label for ordering taxonomies by number of posts in descending order */
label: __( 'Highest #' ),
value: 'count/desc',
},
{
/* translators: label for ordering taxonomies by number of posts in ascending order */
label: __( 'Lowest #' ),
value: 'count/asc',
},
Comment on lines +193 to +202
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure how to label these options.

{
/* translators: label for ordering taxonomies by name in ascending order */
label: __( 'A → Z' ),
value: 'name/asc',
},
{
/* translators: label for ordering taxonomies by name in descending order */
label: __( 'Z → A' ),
value: 'name/desc',
},
];

return (
<TagName { ...blockProps }>
<InspectorControls>
Expand All @@ -202,6 +233,20 @@ export default function CategoriesEdit( {
}
/>
) }
<SelectControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Order by' ) }
options={ orderOptions }
value={ `${ orderBy }/${ order }` }
onChange={ ( value ) => {
const [ newOrderBy, newOrder ] = value.split( '/' );
setAttributes( {
orderBy: newOrderBy,
order: newOrder,
} );
} }
/>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Display as dropdown' ) }
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function render_block_core_categories( $attributes, $content, $block ) {
$args = array(
'echo' => false,
'hierarchical' => ! empty( $attributes['showHierarchy'] ),
'orderby' => 'name',
'orderby' => $attributes['orderBy'],
'order' => $attributes['order'],
'show_count' => ! empty( $attributes['showPostCounts'] ),
'taxonomy' => $attributes['taxonomy'],
'title_li' => '',
Expand Down
4 changes: 3 additions & 1 deletion test/integration/fixtures/blocks/core__categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"showPostCounts": false,
"showOnlyTopLevel": false,
"showEmpty": false,
"showLabel": true
"showLabel": true,
"order": "asc",
"orderBy": "name"
},
"innerBlocks": []
}
Expand Down
Loading