Skip to content

Commit

Permalink
support all options for sticky
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Oct 26, 2020
1 parent 0fbe0c2 commit f9f788d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/block-library/src/query-loop/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function QueryLoopEdit( {
query.exclude = exclude;
}
if ( sticky ) {
query.sticky = sticky;
query.sticky = sticky === 'show';
}
return {
posts: getEntityRecords( 'postType', postType, query ),
Expand Down
22 changes: 14 additions & 8 deletions packages/block-library/src/query-loop/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,27 @@ function render_block_core_query_loop( $attributes, $content, $block ) {
$page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT );

$query = array(
'post_type' => 'post',
'offset' => 0,
'order' => 'DESC',
'orderby' => 'date',
'post_type' => 'post',
'offset' => 0,
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => array(),
);

if ( isset( $block->context['query'] ) ) {
if ( isset( $block->context['query']['postType'] ) ) {
$query['post_type'] = $block->context['query']['postType'];
}
if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) {
$sticky = get_option( 'sticky_posts' );
if ( 'show' === $block->context['query']['sticky'] ) {
$query['post__in'] = $sticky;
} else {
$query['post__not_in'] = array_merge( $query['post__not_in'], $sticky );
}
}
if ( isset( $block->context['query']['exclude'] ) ) {
$query['post__not_in'] = $block->context['query']['exclude'];
$query['post__not_in'] = array_merge( $query['post__not_in'], $block->context['query']['exclude'] );
}
if ( isset( $block->context['query']['perPage'] ) ) {
$query['offset'] = ( $block->context['query']['perPage'] * ( $page - 1 ) ) + $block->context['query']['offset'];
Expand All @@ -56,9 +65,6 @@ function render_block_core_query_loop( $attributes, $content, $block ) {
if ( isset( $block->context['query']['search'] ) ) {
$query['s'] = $block->context['query']['search'];
}
if ( isset( $block->context['query']['sticky'] ) && $block->context['query']['sticky'] ) {
$query['post__in'] = get_option( 'sticky_posts' );
}
}

$posts = get_posts( $query );
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"author": "",
"search": "",
"exclude": [],
"sticky": false
"sticky": ""
}
}
},
Expand Down
17 changes: 12 additions & 5 deletions packages/block-library/src/query/edit/query-inspector-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
TextControl,
FormTokenField,
SelectControl,
ToggleControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { InspectorControls } from '@wordpress/block-editor';
Expand Down Expand Up @@ -100,7 +99,7 @@ export default function QueryInspectorControls( { query, setQuery } ) {
updateQuery.tagIds = [];
}
if ( newValue !== 'post' ) {
updateQuery.sticky = false;
updateQuery.sticky = '';
}
setQuery( updateQuery );
};
Expand All @@ -125,6 +124,14 @@ export default function QueryInspectorControls( { query, setQuery } ) {
onChangeDebounced();
return onChangeDebounced.cancel;
}, [ querySearch, onChangeDebounced ] );
const stickyOptions = useMemo( () => [
{
label: __( 'All posts' ),
value: '',
},
{ label: __( 'Only sticky posts' ), value: 'show' },
{ label: __( 'Exclude sticky posts' ), value: 'exclude' },
] );
return (
<InspectorControls>
<PanelBody title={ __( 'Filtering and Sorting' ) }>
Expand Down Expand Up @@ -176,11 +183,11 @@ export default function QueryInspectorControls( { query, setQuery } ) {
onChange={ setQuerySearch }
/>
{ showSticky && (
<ToggleControl
<SelectControl
options={ stickyOptions }
value={ sticky }
label={ __( 'Sticky' ) }
checked={ sticky }
onChange={ ( value ) => setQuery( { sticky: value } ) }
help={ __( 'Limit results to items that are sticky.' ) }
/>
) }
</PanelBody>
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/fixtures/blocks/core__query.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"author": "",
"search": "",
"exclude": [],
"sticky": false
"sticky": ""
}
},
"innerBlocks": [],
Expand Down

0 comments on commit f9f788d

Please sign in to comment.