Skip to content

Commit

Permalink
Add filtering by author support in Query block (#25149)
Browse files Browse the repository at this point in the history
* add filter by author support in Query block

* provide useSelect dependencies

Co-authored-by: Miguel Fonseca <[email protected]>

* Update packages/block-library/src/query/edit/query-inspector-controls.js

change description

Co-authored-by: Miguel Fonseca <[email protected]>

Co-authored-by: Miguel Fonseca <[email protected]>
  • Loading branch information
ntsekouras and mcsf authored Sep 8, 2020
1 parent 0ba1083 commit 7874839
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
16 changes: 15 additions & 1 deletion packages/block-library/src/query-loop/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default function QueryLoopEdit( {
tagIds = [],
order,
orderBy,
author,
} = {},
queryContext,
},
Expand All @@ -45,6 +46,9 @@ export default function QueryLoopEdit( {
if ( perPage ) {
query.per_page = perPage;
}
if ( author ) {
query.author = author;
}
return {
posts: select( 'core' ).getEntityRecords(
'postType',
Expand All @@ -54,7 +58,17 @@ export default function QueryLoopEdit( {
blocks: select( 'core/block-editor' ).getBlocks( clientId ),
};
},
[ perPage, page, offset, categoryIds, tagIds, order, orderBy, clientId ]
[
perPage,
page,
offset,
categoryIds,
tagIds,
order,
orderBy,
clientId,
author,
]
);

const blockContexts = useMemo(
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/query-loop/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ function render_block_core_query_loop( $attributes, $content, $block ) {
if ( isset( $block->context['query']['perPage'] ) ) {
$query['posts_per_page'] = $block->context['query']['perPage'];
}
if ( isset( $block->context['query']['author'] ) ) {
$query['author'] = $block->context['query']['author'];
}
}

$posts = get_posts( $query );
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"categoryIds": [],
"tagIds": [],
"order": "desc",
"orderBy": "date"
"orderBy": "date",
"author": null
}
}
},
Expand Down
18 changes: 15 additions & 3 deletions packages/block-library/src/query/edit/query-inspector-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@
import { PanelBody, QueryControls } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { InspectorControls } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';

export default function QueryInspectorControls( { query, setQuery } ) {
const { order, orderBy } = query;
const { order, orderBy, author: selectedAuthorId } = query;
const { authorList } = useSelect( ( select ) => {
const { getEntityRecords } = select( 'core' );
return {
authorList: getEntityRecords( 'root', 'user', { per_page: -1 } ),
};
}, [] );
return (
<InspectorControls>
<PanelBody title={ __( 'Sorting' ) }>
<PanelBody title={ __( 'Sorting and filtering' ) }>
<QueryControls
{ ...{ order, orderBy } }
{ ...{ order, orderBy, selectedAuthorId, authorList } }
onOrderChange={ ( value ) => setQuery( { order: value } ) }
onOrderByChange={ ( value ) =>
setQuery( { orderBy: value } )
}
onAuthorChange={ ( value ) =>
setQuery( {
author: value !== '' ? +value : undefined,
} )
}
/>
</PanelBody>
</InspectorControls>
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e-tests/fixtures/blocks/core__query.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"categoryIds": [],
"tagIds": [],
"order": "desc",
"orderBy": "date"
"orderBy": "date",
"author": null
}
},
"innerBlocks": [],
Expand Down

0 comments on commit 7874839

Please sign in to comment.