-
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_authorand 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_editorsand not_sf_authorand 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_urlto 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.
Hi,
I’m using the
sf_input_object_prehook to edit the post type select field in the search form – which works great! I’ve used it to combine post types, for example, people who use my site don’t know the difference between posts and pages, they know them all as “Articles” – so I re-labeled “Posts” as “Articles” and changed the value where the post types get passed as “post,page”. And then I unset the Page option so there wouldn’t be duplication.This works fine, however, since I’m combining post types, the “selected” value that shows once the form is submitted does not show. So “Articles” won’t stay selected after the results are filtered, I guess because the select field is probably just checking one post type against one post type where I’m now passing two. Is it possible to achieve this with the filter hook I’m using? Here is what my function looks like:
function customize_post_type_filter( $input_object, $sfid ) { if ( $input_object['name'] == '_sf_post_type' ) { //update this field before rendering if ( isset( $input_object['options'] ) ) { foreach ( $input_object['options'] as $option ) { //combine posts and pages option if ( $option->label === 'Posts' ) { $option->label = 'Articles'; $option->value = 'post,page'; } } unset( $input_object['options'][2] ); } } return $input_object; }Thanks!