Skip to content

Commit

Permalink
add option to exclude current post from query block
Browse files Browse the repository at this point in the history
  • Loading branch information
g-elwell committed Aug 29, 2024
1 parent 520fa41 commit c213872
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
13 changes: 13 additions & 0 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,16 @@ function gutenberg_pre_init() {

require_once __DIR__ . '/lib/load.php';
}

/**
* This is included for demonstration purposes only.
* We should make this change in the core `build_query_vars_from_query_block` function.
*/
function filter_query_block_exclude_current( $query, $block ) {
if ( isset( $block->context['query']['excludeCurrent'] ) && $block->context['query']['excludeCurrent'] ) {
$query['post__not_in'] = array( get_the_ID() );
}

return $query;
}
add_filter( 'query_loop_block_query_vars', 'filter_query_block_exclude_current', 10, 2 );
1 change: 1 addition & 0 deletions packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@wordpress/date": "file:../date",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
"@wordpress/editor": "file:../editor",
"@wordpress/element": "file:../element",
"@wordpress/escape-html": "file:../escape-html",
"@wordpress/hooks": "file:../hooks",
Expand Down
10 changes: 10 additions & 0 deletions packages/block-library/src/post-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '@wordpress/block-editor';
import { Spinner, ToolbarGroup } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import { list, grid } from '@wordpress/icons';

const TEMPLATE = [
Expand Down Expand Up @@ -85,6 +86,7 @@ export default function PostTemplateEdit( {
author,
search,
exclude,
excludeCurrent,
sticky,
inherit,
taxQuery,
Expand All @@ -109,6 +111,7 @@ export default function PostTemplateEdit( {
( select ) => {
const { getEntityRecords, getTaxonomies } = select( coreStore );
const { getBlocks } = select( blockEditorStore );
const { getCurrentPostId } = select( editorStore );
const templateCategory =
inherit &&
templateSlug?.startsWith( 'category-' ) &&
Expand Down Expand Up @@ -160,6 +163,13 @@ export default function PostTemplateEdit( {
if ( exclude?.length ) {
query.exclude = exclude;
}
if ( excludeCurrent ) {
if ( query.exclude ) {
query.exclude = [ ...query.exclude, getCurrentPostId() ];
} else {
query.exclude = [ getCurrentPostId() ];
}
}
if ( parents?.length ) {
query.parent = parents;
}
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 @@ -25,7 +25,8 @@
"sticky": "",
"inherit": true,
"taxQuery": null,
"parents": []
"parents": [],
"excludeCurrent": false
}
},
"tagName": {
Expand Down
19 changes: 19 additions & 0 deletions packages/block-library/src/query/edit/inspector-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
Notice,
ToggleControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
Expand Down Expand Up @@ -58,6 +59,7 @@ export default function QueryInspectorControls( props ) {
inherit,
taxQuery,
parents,
excludeCurrent,
} = query;
const allowedControls = useAllowedControls( attributes );
const [ showSticky, setShowSticky ] = useState( postType === 'post' );
Expand Down Expand Up @@ -119,6 +121,8 @@ export default function QueryInspectorControls( props ) {
! inherit &&
showSticky &&
isControlAllowed( allowedControls, 'sticky' );
const showExcludeCurrentControl =
! inherit && isControlAllowed( allowedControls, 'excludeCurrent' );
const showSettingsPanel =
showInheritControl ||
showPostTypeControl ||
Expand Down Expand Up @@ -262,6 +266,21 @@ export default function QueryInspectorControls( props ) {
}
/>
) }
{ showExcludeCurrentControl && (
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Exclude current post' ) }
help={ __(
'Exclude the current post from the query.'
) }
checked={ excludeCurrent }
onChange={ ( value ) => {
setQuery( {
excludeCurrent: !! value,
} );
} }
/>
) }
<EnhancedPaginationControl
enhancedPagination={ enhancedPagination }
setAttributes={ setAttributes }
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/query/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const settings = {
search: '',
sticky: 'exclude',
inherit: false,
excludeCurrent: false,
},
},
innerBlocks: [
Expand Down

0 comments on commit c213872

Please sign in to comment.