Query groups by incomplete name

Posted in

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 )
echo json_encode( $return );

One Response to Query groups by incomplete name

  1. George September 25, 2018 at 12:43 pm #

    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,

We use cookies to optimize your experience on our site and assume you're OK with that if you stay.
OK, hide this message.

Affiliates · Contact · Jobs · Terms & Conditions · Privacy Policy · Documentation · Downloads · Useful Plugins · My Account