diff --git a/packages/edit-site/src/components/post-list/index.js b/packages/edit-site/src/components/post-list/index.js index a0d5c191ccce41..3e083cd3c31f8c 100644 --- a/packages/edit-site/src/components/post-list/index.js +++ b/packages/edit-site/src/components/post-list/index.js @@ -192,6 +192,7 @@ function getItemId( item ) { export default function PostList( { postType } ) { const [ view, setView ] = useView( postType ); + const [ isDirty, setIsDirty ] = useState( false ); const defaultViews = useDefaultViews( { postType } ); const history = useHistory(); const location = useLocation(); @@ -215,11 +216,27 @@ export default function PostList( { postType } ) { }, [ location.path, location.query.isCustom, history ] ); - const getActiveViewFilters = ( views, match ) => { const found = views.find( ( { slug } ) => slug === match ); return found?.filters ?? []; }; + useEffect( () => { + if ( isDirty ) { + return; + } + + // The sort defaults are set in DEFAULT_POST_BASE. + if ( view.sort.field !== 'title' || view.sort.direction !== 'asc' ) { + setIsDirty( true ); + setView( { + ...view, + layout: { + ...( view?.layout || {} ), + hierarchicalSort: false, + }, + } ); + } + }, [ isDirty, view ] ); const { isLoading: isLoadingFields, fields: _fields } = usePostFields( { postType, @@ -296,8 +313,7 @@ export default function PostList( { postType } ) { _embed: 'author', order: view.sort?.direction, orderby: view.sort?.field, - orderby_hierarchy: - view.type === 'table' && !! view.layout?.hierarchicalSort, + orderby_hierarchy: !! view.layout?.hierarchicalSort, search: view.search, ...filters, }; diff --git a/packages/edit-site/src/components/sidebar-dataviews/default-views.js b/packages/edit-site/src/components/sidebar-dataviews/default-views.js index 38f809756828db..8ef4c50483687b 100644 --- a/packages/edit-site/src/components/sidebar-dataviews/default-views.js +++ b/packages/edit-site/src/components/sidebar-dataviews/default-views.js @@ -27,7 +27,11 @@ import { } from '../../utils/constants'; export const defaultLayouts = { - [ LAYOUT_TABLE ]: {}, + [ LAYOUT_TABLE ]: { + layout: { + hierarchicalSort: true, + }, + }, [ LAYOUT_GRID ]: {}, [ LAYOUT_LIST ]: {}, };