https://developer.wordpress.org/refere…uery_vars
Frontend:
function set_query_for_documents_list($query, $block)
{
$current_post_id = get_the_id();
$current_post_type = get_post_type($current_post_id);
if ($current_post_type === 'textos') {
$documents_flag = get_field('documents_flag', $current_post_id);
if ($documents_flag) {
$documents = get_field('documents', $current_post_id);
$rule = $documents['selection_rule'];
if ($rule === 'typology') {
$terms_ids = $documents['terms'];
$query['tax_query'] = [[
'taxonomy' => 'tipologias',
'field' => 'term_id',
'terms' => $terms_ids,
]];
} else if ($rule === 'manual') {
$documents_ids = $documents['selection'];
$query['post__in'] = $documents_ids;
}
}
}
return $query;
}
add_filter('query_loop_block_query_vars', 'wpdocs_randomize_query', 10, 2);Editor:
add_filter('rest_listas_ficheiros_query', 'wpfieldwork_rest_upcoming_events', 10, 2);
function set_editor_query_for_documents_list($args, $request)
{
$customFields = $request['customFields'];
if ($customFields) {
$referer_url = wp_get_referer();
$queryString = parse_url($referer_url, PHP_URL_QUERY);
parse_str($queryString, $params);
$current_post_id = $params['post'];
$current_post_type = get_post_type($current_post_id);
if ($current_post_type === 'textos') {
$documents_flag = get_field('documents_flag', $current_post_id);
if ($documents_flag) {
$documents = get_field('documents', $current_post_id);
$rule = $documents['selection_rule'];
if ($rule === 'typology') {
$terms_ids = $documents['terms'];
$args['tax_query'] = [[
'taxonomy' => 'tipologias',
'field' => 'term_id',
'terms' => $terms_ids,
]];
} else if ($rule === 'manual') {
$documents_ids = $documents['selection'];
$args['post__in'] = $documents_ids;
}
}
}
}
return $args;
}JS:
const postType = useSelect(
(select) => select("core/editor").getCurrentPostType(),
[],
);
const postPostId = useSelect(
(select) => select("core/editor").getCurrentPostId(),
[],
);
const postRecords = useSelect(
(select) => {
return select("core").getEntityRecords(
"postType",
postType,
postPostId,
);
},
[postType, postPostId],
);
let documentsFlag;
let documents;
if (postRecords !== null) {
const postData = postRecords[0];
const acfFields = postData.acf;
documentsFlag = acfFields.documents_flag;
documents = acfFields.documents;
}
useEffect(() => {
if (documents !== null && documentsFlag) {
const parentBlockId = select("core/block-editor").getBlockRootClientId(
props.clientId,
);
const parentParentBlockId =
select("core/block-editor").getBlockRootClientId(parentBlockId);
const rule = documents.selection_rule;
let query = {
postType: props.context.postType,
};
console.log(props.context.postType);
if (rule === "typology") {
query.taxQuery = {
tipologias: documents.terms,
};
} else if (rule === "manual") {
query.include = documents.selection;
}
dispatch("core/block-editor").updateBlockAttributes(parentParentBlockId, {
query,
});
}
}, [documents, documentsFlag]);Note: When defining the code on the server side, changes are not reactive in the block editor. To achieve an immediate effect, I recommend using Gutenberg’s built-in methods (see link), inside a useEffect.
