<PostSelector />
Lets you select one or multiple posts, and returns the selected post object.
Example
import { PostSelector } from '@t2/editor';
<PostSelector
allowMultiple
onSelect={selectedPosts => setAttributes({ posts: selectedPosts })}
/>
Available props
| Prop | Type | Description |
|---|---|---|
| postType? | string; | The posttype to pick from. |
| selectedPostTypes? | string[]; | Selected posttypes to pick from. |
| allowMultiple? | boolean; | Whether to allow multiple posts to be selected. |
| onSelect | onSelect: (args1: WordPressSearchPost | WordPressSearchPost[]) => void; | An onSelect function giving you access to the selected post object, or an array of posts |
| onRequestClose? | () => void; | A function to call when the modal closes |
| disabledIds? | number[]; | An array of post IDs that should be disabled. |
| selectedPosts? | WordPressSearchPost[]; | An array of selected posts. |
Multisite
Read more about multisite support in the Multisite documentation.
Filters
JavaScript
T2.PostSelector.data
Filters the request data before it's sent to the post selector search endpoint.
import { addFilter } from '@wordpress/hooks';
addFilter(
'T2.PostSelector.data',
'my-plugin/post-selector-data',
(data) => {
// Modify the request data
return {
...data,
// Add custom parameters
};
}
);
| Parameter | Type | Description |
|---|---|---|
data |
object |
The request data containing subtype, search, terms, and include. |
PHP
t2/editor/post_selector/query_args
Filters the WP_Query arguments for the post selector search.
add_filter(
't2/editor/post_selector/query_args',
function ( array $query_args, WP_REST_Request $request ): array {
// Modify the query arguments
return $query_args;
},
10,
2
);
| Parameter | Type | Description |
|---|---|---|
$query_args |
array |
The WP_Query arguments. |
$request |
WP_REST_Request |
The REST request object. |
t2/editor/post_selector/single_item_response
Filters the single item response data.
add_filter(
't2/editor/post_selector/single_item_response',
function ( array $value ): array {
// Add custom fields to the response
return $value;
}
);
| Parameter | Type | Description |
|---|---|---|
$value |
array |
The single item response containing id, title, subtype, and siteId. |
Polylang support
First enqueue the current language to the editor.
if (function_exists('pll_current_language')) {
\wp_localize_script( 'dinutvei-theme', 'pll',
[
'lang' => \pll_current_language(),
]
);
}
Then add the filter to the post selector in your theme's editor script.
import { addFilter } from '@wordpress/hooks';
addFilter('T2.PostSelector.data', 'project-name/post-selector-language', (data) => {
if (window.pll?.lang) {
return {
...data,
lang: window.pll.lang,
};
}
return data;
});
Then add the filter to the single item response.
/**
* Filter post selector query to only show posts in the current language.
*
* @param array $query_args The WP_Query arguments.
* @param \WP_REST_Request $request The REST request object.
*
* @return array
*/
function filter_post_selector_by_language( array $query_args, \WP_REST_Request $request ): array {
$lang = $request->get_param( 'lang' );
$subtype = $request->get_param( 'subtype' );
$subtypes = $subtype ? explode( ',', $subtype ) : [];
// Check if any of the requested post types are translatable.
$has_translatable_type = false;
if ( function_exists( 'pll_is_translated_post_type' ) ) {
foreach ( $subtypes as $post_type ) {
if ( $post_type && pll_is_translated_post_type( $post_type ) ) {
$has_translatable_type = true;
break;
}
}
}
if ( $lang && $has_translatable_type ) {
$query_args['lang'] = sanitize_text_field( $lang );
}
return $query_args;
}