-
Search Results
-
Topic: Author field too big to load
The Problem: I need to load an author dropdown on the filter with just the Editors
I have a Filter for my site, with several fields, one of them is Author, but the website has more than 15.000 users registered and get a timeout error every time the field is enabled.
I’ve tried this to re write the author field with a more manageable amount of users (only the Editors) but with no luck the idea was to remove all content from
_sf_author
and replace it with the editors`
function filter_authors($input_object, $sfid)
{
if (($input_object[‘name’] != ‘_sf_author’) || ($input_object[‘type’] != ‘select’)) {
return $input_object;
}unset($input_object[‘options’]);
$input_object[‘options’] = array();// Generate first array option (default)
$first_option = new StdClass();
$first_option->value = ”;
$first_option->label = ‘All Authors’;//attributes
$first_option->attributes = array(
‘title’ => ‘All Authors’,
);array_push($input_object[‘options’], $first_option);
//change classes & attributes of the field
$input_object[‘attributes’][‘class’] = ‘user_filter’;
$input_object[‘attributes’][‘title’] = ‘Authors’;//add/override prefix & postfix to the field
$input_object[‘prefix’] = “Filter by Author”;//Check if options variable exists
if (!isset($input_object[‘options’])) {
return $input_object;
}//Create new users array
$args = array(
‘role’ => ‘editor’,
‘order’ => ‘ASC’,
‘orderby’ => ‘display_name’,
);// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
foreach ($authors as $author) {
// get all the user’s data
$author_info = get_userdata($author->ID);
//create new options with user values
$new_option = new StdClass();
$new_option->value = $author_info->user_login;
$new_option->label = $author_info->first_name . ‘ ‘ . $author_info->last_name;//attributes
$new_option->attributes = array(
‘title’ => $author_info->user_login,
);
array_push($input_object[‘options’], $new_option);
}
return $input_object;
}
// add_filter(‘sf_input_object_pre’, ‘filter_authors’, 10, 2);
`
Using this approach i change a meta field and filled it with the Editors data, but then the post request was send as_sfm_editors
and not_sf_author
and i couldn’t find a way to replace this request for the one i needed.Also tried building my own query accessing the search data and using the hook
sf_results_url
to change the url, but i cannot change the search query with it.I’m running out of ideas, appreciate your time to check my issue and any new ideas on how to solve it.