How to get an array of providers IDs?

Hello!

I need to create a nice looking list of experts (providers) which links to their bio-page where then the people can book a service from them.

I know how to make the bio pages and display the calendar and the services dropdown related to that provider.

what I'd need is an easy way to get an array of experts IDs and their related bio-pages IDs, so then I can loop trough it and display their name, the image of the page, the excerpt/description and eventually other fields (like a plain text list of the services they provide).

is there any function which can help me out here?
I had a quick look and I've seen I could do a mysql query and join the app_workers table with the usermeta, but I was wondering if there was an easier and quicker way..

especially because then I'd also need to create other loops which list the providers of a given service (like you click "business" and you can see only the providers which can do that service), and I'm not sure if I'll be able to do this with a mysql query, as I'm not very expert about them..

Any suggestion? :slight_smile:

Thanx for your help!
Andrea

  • Vaughan

    Hi @andrea,

    Appointments has a function which might do what you ask.

    /**
    	 * Get all workers
    	 * @param order_by: ORDER BY clause for mysql
    	 * @return array of objects
    	 */
    	function get_workers( $order_by="ID" ) {
    		$order_by = $this->sanitize_order_by( $order_by );
    		$workers = wp_cache_get( 'all_workers_' . str_replace( ' ', '_', $order_by ) );
    		if ( false === $workers ) {
    			// Sorting by name requires special case
    			if ( stripos( $order_by, 'name' ) !== false ) {
    				$workers_ = $this->db->get_results("SELECT * FROM " . $this->workers_table . " " );
    				if ( stripos( $order_by, 'desc' ) !== false )
    					usort( $workers_, array( &$this, 'get_workers_desc' ) );
    				else
    					usort( $workers_, array( &$this, 'get_workers_asc' ) );
    				$workers = $workers_;
    			}
    			else
    				$workers = $this->db->get_results("SELECT * FROM " . $this->workers_table . " ORDER BY ".$order_by ." " );
    			wp_cache_set( 'all_workers_' . str_replace( ' ', '_', $order_by ), $workers );
    		}
    		return $workers;
    	}

    So if you just call that function, it should return you a list of object worker/provider objects.

    Hope this helps

  • Andrea

    That was a super quick reply! :slight_smile:
    Thank you very much, it looks very handy!

    which are exactly the info I'll get within those workers objects?
    I assume:
    ID -> user ID
    page -> ID of worker bio page
    services -> array of services provided by this worker (how I retrieve this? is it encapsulated?)

    is there anything else?

    and is there a way to filter those workers and retrive only the ones who provide a particular service?

    Thanx!
    Andrea

  • Andrea

    Hi guys!

    I've got really inspired today and I've found a way to do it ^_^

    I've made this custom function:

    function get_providers_by_service( array $filter_service_ID_list  ) {
    
    	// for each given ID, add the WHERE argument for the query to search for that ID into the services_provided column
    
    	$search_service_args ='';
    	foreach ($filter_service_ID_list as $key => $filter_service_ID){
    		if ($key>0) {
    			$search_service_args .=	" OR "; // if it's not the first ID, add the relational direction (you can put AND as well, but remember the empty spaces!)
    		}
    		$search_service_args .=	"services_provided LIKE '%:".$filter_service_ID.":%'";
    	}
    
    	// query the database
    
    	global $wpdb;
    
    	$results = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "app_workers" . " WHERE ".$search_service_args."");
    
    	return $results;
    
    }

    basically you can pass one or more service id in the function and you will get an array of objects for the workers which are providing any of those services.
    if you change the " OR " in the function (see the comment) and put " AND " instead, you can get only the workers which are providing ALL the given services.

    this is an example of how to use it:

    $workers_list = get_providers_by_service( array(4, 2, 3) );
    
    	foreach ($workers_list as $worker_data) {
    		// get all the user info, so we can print name and everything else
    		$worker_meta_data = get_userdata( $worker_data->ID );
    
    		echo "name: ".$worker_meta_data->user_firstname."<br/>";
    		echo "surname: ".$worker_meta_data->user_lastname."<br/>";
    
    		// we can also print the thumb from his bio page
    		echo get_the_post_thumbnail( $worker_data->page );
    		echo "<br/>";
    
    		//or get the entire page object so we can print whatever
    		$worker_page_data = get_post( $worker_data->page );
    
    }

    I hope this will help someone else! ^_^

    Thanx Vaughan for your help anyway and thanx to WPMU for coding such clear and tidy plugins which is always a pleasure to work with. :slight_smile:

    cheers!
    Andrea

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.