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 filtering by author support in Query block #25149

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 } ),
};
} );
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
return (
<InspectorControls>
<PanelBody title={ __( 'Sorting' ) }>
<PanelBody title={ __( 'Sorting and Filtering' ) }>
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
<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