CMB2-Post-Search-field
CMB2-Post-Search-field copied to clipboard
Remember selection (code inside)
Hi, I wrote a few lines of code, that rechecks selected items
Add the code in init.php after line 188 (search.$response.html(data);)
search.$response.find('input').each(function (k, v) {
if (search.$idInput.val().split(',').indexOf(v.value) >= 0) {
jQuery(v).prop('checked', true);
}
});
updated comment, it is line 188, not 199
For multiple selection: An id of each element must be trimmed otherwise only first checkbox will be selected:
let postIds = $.map(search.$idInput.val().split(","), $.trim); search.$response.find('input').each(function ( k, v ) {
if ( postIds.indexOf( v.value ) >= 0 ) {
$(v).prop('checked', true);
}
});
@jtsternberg
I think this should be in CMB2-Post-Search-field
plugin core.
The only issue so far: by default WP loads only 50 items in the list. So if you have more and the one selected is beyond those 50 default selected - they won't be display and selected (and when you click Submit - ignored).
We need to alter this behavior, so at the top of the list we will always load the selected items, and after that - those that are left. We will need to use pre_get_posts
, as you have examples here: https://github.com/CMB2/CMB2-Post-Search-field/issues/21#issuecomment-235641777 - for post statuses.