Skip to content

Commit 272d226

Browse files
committed
fix fat query behavior
1 parent 00f64e0 commit 272d226

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

inc/class.admin.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,13 @@ public function MetaboxOriginalContent( $post ) {
273273

274274
$current_options = get_option( SPTRANS_OPTIONS_NAME );
275275

276-
// List all other content
276+
// List initial content (limited for performance)
277+
// The AJAX version will handle full search capabilities
277278
$q_all_content = new WP_Query(
278279
[
279280
'post_type' => ! empty( $current_options['cpt'] ) ? $current_options['cpt'] : 'any',
280281
'post_status' => 'any',
281-
'posts_per_page' => 2000,
282+
'posts_per_page' => 100,
282283
'no_found_rows' => true,
283284
'post__not_in' => [ $current_parent_id ],
284285
'orderby' => 'title',
@@ -290,6 +291,7 @@ public function MetaboxOriginalContent( $post ) {
290291
echo '<option value="' . esc_attr( $object->ID ) . '">' . esc_html( $object->ID ) . ' - ' . esc_html( $object->post_title ) . '</option>' . "\n";
291292
}
292293
}
294+
wp_reset_postdata();
293295
?>
294296
</select>
295297
</div>

ressources/admin.js

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ jQuery(document).ready(function () {
2222
checkUnicityTranslation(jQuery(this).val());
2323
});
2424

25+
// Store search timeout globally to avoid conflicts
26+
let searchTimeout = null;
27+
2528
function loadPostParentAjax(post_type, current_value) {
2629
jQuery("select#post_parent_js").load(ajaxurl, {
2730
'action': 'load_original_content',
2831
'post_type': post_type,
29-
'current_value': current_value
32+
'current_value': current_value,
33+
'limit': 100
3034
}, function (response, status, xhr) {
3135
if (status == "error") {
3236
alert("Sorry but an error occured with AJAX method");
@@ -35,12 +39,103 @@ jQuery(document).ready(function () {
3539
if (translationSelect !== null) {
3640
translationSelect.destroy();
3741
}
42+
3843
// Initialize Choices.js after options are loaded
39-
translationSelect = new Choices(jQuery('#post_parent_js')[0], {
44+
const selectElement = jQuery('#post_parent_js')[0];
45+
translationSelect = new Choices(selectElement, {
4046
searchEnabled: true,
4147
itemSelectText: '',
48+
searchChoices: false, // Disable local search, we'll use AJAX
49+
shouldSort: true,
50+
shouldSortItems: true
4251
});
52+
53+
// Wait for Choices.js to render, then attach search handler
54+
setTimeout(function() {
55+
attachSearchHandler(post_type, current_value);
56+
}, 100);
57+
58+
syncSelectParentBox();
59+
}
60+
});
61+
}
62+
63+
function attachSearchHandler(post_type, current_value) {
64+
// Find the search input in Choices.js
65+
const choicesContainer = jQuery('#post_parent_js').closest('.choices');
66+
const searchInput = choicesContainer.find('input[type="text"]');
67+
68+
// Remove any existing handlers
69+
searchInput.off('input.ajaxSearch');
70+
71+
// Attach new search handler
72+
searchInput.on('input.ajaxSearch', function() {
73+
const searchTerm = jQuery(this).val().trim();
74+
75+
// Clear previous timeout
76+
if (searchTimeout) {
77+
clearTimeout(searchTimeout);
78+
}
79+
80+
// If search is cleared, reload initial list
81+
if (searchTerm.length < 2) {
82+
loadPostParentAjax(post_type, current_value);
83+
return;
84+
}
85+
86+
// Debounce search requests (wait 300ms after user stops typing)
87+
searchTimeout = setTimeout(function() {
88+
loadPostParentAjaxSearch(post_type, current_value, searchTerm);
89+
}, 300);
90+
});
91+
}
92+
93+
function loadPostParentAjaxSearch(post_type, current_value, searchTerm) {
94+
jQuery.ajax({
95+
url: ajaxurl,
96+
type: 'POST',
97+
data: {
98+
'action': 'load_original_content',
99+
'post_type': post_type,
100+
'current_value': current_value,
101+
'search': searchTerm,
102+
'limit': 100
103+
},
104+
success: function(response) {
105+
// Parse the response to get options
106+
const tempSelect = jQuery('<select>').html(response);
107+
const options = tempSelect.find('option');
108+
109+
// Destroy and recreate Choices instance with new options
110+
if (translationSelect !== null) {
111+
translationSelect.destroy();
112+
}
113+
114+
// Clear and repopulate select
115+
const selectElement = jQuery('#post_parent_js')[0];
116+
selectElement.innerHTML = '';
117+
options.each(function() {
118+
selectElement.appendChild(this);
119+
});
120+
121+
// Reinitialize Choices.js
122+
translationSelect = new Choices(selectElement, {
123+
searchEnabled: true,
124+
itemSelectText: '',
125+
searchChoices: false,
126+
shouldSort: true,
127+
shouldSortItems: true
128+
});
129+
130+
// Reattach search handler
131+
setTimeout(function() {
132+
attachSearchHandler(post_type, current_value);
133+
}, 100);
134+
43135
syncSelectParentBox();
136+
},
137+
error: function() {
138+
alert("Sorry but an error occured with AJAX search method");
44139
}
45140
});
46141
}

0 commit comments

Comments
 (0)