Your cart is currently empty!
Query groups by incomplete name
—
by
We have 160 groups and 3800 users. The Email Users plugin is trying to load all groups and users on page load. We are trying to integrate select2.js so create a searchable select field using ajax as demoed here https://select2.org/data-sources/ajax
Further instructions here https://rudrastyh.com/wordpress/select2-for-metaboxes-with-ajax.html
My question is how would we format the query?
Here is an example but it uses posts…
add_action( 'wp_ajax_mishagetposts', 'rudr_get_posts_ajax_callback' ); // wp_ajax_{action}
function rudr_get_posts_ajax_callback(){
// we will pass post IDs and titles to this array
$return = array();
// you can use WP_Query, query_posts() or get_posts() here – it doesn’t matter
$search_results = new WP_Query( array(
‘s’=> $_GET[’q’], // the search query
‘post_status’ => ‘publish’, // if you don’t want drafts to be returned
‘ignore_sticky_posts’ => 1,
‘posts_per_page’ => 50 // how much to show at once
) );
if( $search_results->have_posts() ) :
while( $search_results->have_posts() ) : $search_results->the_post();
// shorten the title a little
$title = ( mb_strlen( $search_results->post->post_title ) > 50 ) ? mb_substr( $search_results->post->post_title, 0, 49 ) . ‘…’ : $search_results->post->post_title;
$return[] = array( $search_results->post->ID, $title ); // array( Post ID, Post Title )
endwhile;
endif;
echo json_encode( $return );
die;
}
Comments
One response to “Query groups by incomplete name”
Hi Lawrence,
Please have a look at get_groups() and the description on the API.
Also, if you noticed the select tag in your Dashboard under Users where you can multi-select Groups, the implementation can be found in groups/lib/admin/class-groups-admin-users.php.
Hope these help you with your implementation.
Kind regards,
George