This plugin introduces a Query Loop block variation that will empower users to be able to do much more complicated queries with the Query Loop block, such number of posts to display and post meta
Please use the either the support forum or the official repository for any questions or to log issues.
Select additional post types for your query!
Set the number of items you want to display (up to 50).
Choose whether you want to start at the first or 100th!
Generate complicated post meta queries using an interface that allows you to create a query based on meta_key
, meta_value
and the compare
options. Combine multiple queries and determine if they combine results (OR) or narrow them down (AND).
Query item before a date, after a date or between two dates!
Sort in ascending or descending order by:
- Author
- Date
- Last Modified Date
- Title
- Meta Value
- Meta Value Num
- Random
- Menu Order ( props to @jvanja )
Please note that this is a slight duplication of the existing sorting controls. They both work interchangeably but it just looks a bit odd in the UI
Since version 1.5, AQL is now completely extendable. Using the SlotFills and filter outlined below, you can add any custom control you may need!
There are two SlotFills available to extend the UI of AQL that are exposed based the value of the Inherit query from template
setting of the block.
The purpose of having two options is to be able to customize when a UI element is added. There may be cases that a particular control doesn't make sense to be shown when the query is being inherited.
For example, a control that makes changes to the content types being displayed may not make sense when used in an archive template and so that control would only be added using the <AQLControls
SlotFill so that it doesn't appear when Inherit query from template
is enabled.
- AQLControls
- AQLControlsInheritedQuery
Both SlotFills are passed all props
from the main block and are available on the window.aql
object for use.
The example below adds a new control to only show content from the from currently logged in user regardless of the status of Inherit query from template
.
const { AQLControls, AQLControlsInheritedQuery } = window.aql;
import { registerPlugin } from '@wordpress/plugins';
import { ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
const LoggedInUserControl = ( { attributes, setAttributes } ) => {
const { query: { authorContent = false } = {} } = attributes;
return (
<>
<ToggleControl
label={ __( 'Show content for logged in user only' ) }
checked={ authorContent === true }
onChange={ () => {
setAttributes( {
query: {
...attributes.query,
authorContent: ! authorContent,
},
} );
} }
/>
</>
);
};
registerPlugin( 'aql-extension', {
render: () => {
return (
<>
<AQLControls>
{ ( props ) => <LoggedInUserControl { ...props } /> }
</AQLControls>
<AQLControlsInheritedQuery>
{ ( props ) => <LoggedInUserControl { ...props } /> }
</AQLControlsInheritedQuery>
</>
);
},
} );
Once the control is in place and saving, you will need to use that new query variable to modify the underlying WP_Query
instance via the aql_query_vars
filter.
The filter provides three parameters:
$query_args
Arguments to be passed to WP_Query.$block_query
The query attribute retrieved from the block.$inherited
Whether the query is being inherited.
The example code below modifies the query based on the status of the control added above.
/**
* Add a filter to only show logged-in user content.
*
* @param array $query_args Arguments to be passed to WP_Query.
* @param array $block_query The query attribute retrieved from the block.
* @param boolean $inherited Whether the query is being inherited.
*/
function aql_extension_show_current_author_only( $query_args, $block_query, $inherited ) {
if (
isset( $block_query['authorContent'] ) &&
true === filter_var( $block_query['authorContent'], FILTER_VALIDATE_BOOLEAN )
) {
$query_args['author'] = get_current_user_id();
}
return $query_args;
}
\add_filter( 'aql_query_vars', 'aql_extension_show_current_author_only', 10, 3 );