RESTAPI and read() - Filter and Arguments with Multiple Parameters

Reviewing API documentation for read(), I see “almost any argument/value pair you might use as a widget argument will work with the REST API”.

What about arguments with additional parameters?

  • <arg id="filter" name="profiles_315" action="equals">Major</arg>
  • <arg id="filter" name="profiles_315" action="equals">Minor</arg>
  • <arg id="filter" name="profiles_315" action="equals">Program</arg>
  • <arg id="filter_mode">any</arg>

Is it possible to use arguments like this in RESTAPI and read()? If so, how?

Hi Nick,

Indeed you can! We’ve fleshed out the documentation for the filter setting to better explain this.

The gist is, you use | to separate the field|operator|value checks for each filter setting. So, in your example, the REST API request would be something like

/filter/profiles_315|equals|Major
/filter/profiles_315|equals|Minor
/filter/profiles_315|equals|Program
/filter_mode/any

(Line breaks added for clarity – you’d chain these together all into one URL, of course.)

Hope this helps!

Thanks for the direction on the right format, Karl. I was able to get the desired behavior in a REST API call via a URL.

However, the multiple filter arguments don’t seem to act as expected when used in $LW->read(). If I have an $args array like this:

$args = [
     ‘type’=>$requirement_type_name,
     ‘paginate’ => 999,
     ‘filter_mode’ => ‘any’,
     ‘filter’ => ‘profiles_315|equals|Major’,
     ‘filter’ => ‘profiles_315|equals|Minor’,
     ‘filter’ => ‘profiles_315|equals|Program’,
];

the results only return the profiles with Program, rather than all three types. In hindsight, that’s to be expected, defining multiple arguments with the same key in an array.

I tried to merge the three arguments into a single line, but nothing I did returned any results:

  • ‘filter’ => ‘profiles_315|equals|Major|profiles_315|equals|Minor|profiles_315|equals|Program’
  • ‘filter’ => ‘profiles_315|equals|Major,profiles_315|equals|Minor,profiles_315|equals|Program’
  • ‘filter’ => ‘profiles_315|equals|Major/profiles_315|equals|Minor/profiles_315|equals|Program’

Is there a way to merge multiple filters into a single line in this context, or are there different keys which are accepted as “filter”, or any similar solution?


Aside: I also tried to “hack” it like this, but it seems read() doesn’t use the RESTAPI URL as its data source. Likely too confusing and unsustainable to be worth using, even if it did work.

‘filter’ => ‘profiles_315|equals|Major/filter/profiles_315|equals|Minor/filter/profiles_315|equals|Program’,

Pending an all-in-one solution, allow me to share this alternative get-each-and-combine solution for anyone who many have a similar need.

function getRequirements() {

	// Invoke LiveWhale.
	global $_LW;
	
	// Parameters.
	$data_type = 'profiles';
	$types_to_use = ['Major','Minor','Program'];

	// Query LW for requirements data.
	// No caching here, given this is part of a larger process.
	$args = [
		'type'=>'Major & Minor Requirements',
		'paginate' => 999,
		'exclude_tag' => 'exclude-from-majors-minors-page',
	];

	// Can't get all three at once, so must get separately...
	$results = [];
	foreach ($types_to_use as $type) {
		$args['filter'] = 'profiles_315|equals|'.$type;
		$results[$type] = $_LW->read($data_type, $args);
	}

	//...and merge into one.
	$requirements = [];
	foreach ($results as $set) { // avoids overlapping keys (0,1,2...)
		foreach ($set as $profile) {
			$requirements[$profile['id']] = $profile; // take opportunity to use id as key
		}
	}
	
	// Return array
	return $requirements;
	
}

Good point, I think you’re running into a limitation of PHP that doesn’t let you set multiple key=>value pairs with the same key.

Does it work if you try an array? Might be worth trying:

$args = [
    'type'=>$requirement_type_name,
    'paginate' => 999,
    'filter_mode' => 'any',
    'filter' => ['profiles_315|equals|Major', 'profiles_315|equals|Minor', 'profiles_315|equals|Program']`
];

Yup, passing along the various filters in an array appears to have worked as desired.

'filter' => ['profiles_315|equals|Major', 'profiles_315|equals|Minor', 'profiles_315|equals|Program'],

Thanks.

1 Like