Skip to content

List posts

corsacca edited this page Apr 15, 2020 · 8 revisions

Get a list of contacts, groups or another post type, with filtering and sorting parameters

Endpoint

GET https://example.com/wp-json/dt-posts/v2/{post_type}/

Parameters

sort (string)

  • Options:
    • name //name or title of the record
    • assigned_to
    • last_modified //date of the last comment or edit
    • post_date //creation date of the record
  • Options for contacts:
    • overall_status
    • seeker_path
    • faith_milestones
    • groups
  • Options for groups:
    • leaders
    • group_status
    • group_type
    • members

Add a - before any of these options to return them in descending order

Example:

// get records assigned to me, ordered by creation date from newest to oldest
let searchParameters = {
  assigned_to: [ 'me' ],
  sort: `-post_date`
}

assigned_to (array) of presets or ids to filter:

  • all // default for dispather
  • me // default for multiplier
  • shared // records that have been shared with me
  • 83 // records assigned to user 83
  • 84

Example:

// get records assigned to me
let searchParameters = {
  assigned_to: [ 'me' ]
}
// get records assigned_to user 22 and user 48
let searchParameters = {
  assigned_to: [ 22, 48 ]
}

key_select, multi_select (array)

  • overall_stats
  • milestones
  • age
  • etc

Build as an array of the option keys to filter to.

Example:

// get contacts that have the 'Has Bible' or 'Reading Bible' milestones and that are at the 'Meeting Scheduled' stage.
let searchParameters = {
  milestones: [ 'milestone_has_bible', 'milestone_reading_bible' ],
  seeker_path: [ 'scheduled' ]
}

connections (array)

  • subassigned

Build as an array of ids filter to.

Example:

// get contacts subassigned to contact 93
let searchParameters = {
  subassinged: [ 93 ]
}

For the subassigned filter, add a combine:[ 'subassigned' ] when filtering for an assigned_to to search for either the assigned_to OR the subassigned. Otherwise it will search for contacts that are assigned to the selected user and subassigned to the select contact. Example:

// get contacts assigned_to user 22 **OR** subassigned to contact 93
let searchParameters = {
  assigned_to [ 22 ],
  subassinged: [ 93 ],
  combine: [ 'subassigned' ]
}

location

  • location_grid

Example:

// get contacts in location with location_grid (in the dt_location_grid table grid_id) id 123456
let searchParameters = {
  location_grid: [ 123456 ]
}

date

  • created_on // date the record was created
  • baptism_date
  • etc

Example:

// get the records created between in 2018
let searchParameters = {
  created_on : {
    start: "2018-01-01",
    end: "2019-01-01"
  }
}
// get contacts baptized before Feb 2019
let searchParameters = {
  baptism_date : {
    end: "2019-02-01"
  }
}

Boolean (array)

  • requires_update
  • etc

"1" for true, "0" for false

Example:

// get contacts subassigned to contact 93
let searchParameters = {
  requires_update: [ "1" ]
}

Search Parameters

text (string) searches contact or group names/titles and contact information (email, phone etc)

Example:

// search for "Bob"
let searchParameters = {
  text: "Bob"
}

Paging Parameters

offset (integer) the number of records to skip. Optional. limit (integer) the number of records to include in the response. Default is 100. Warning: a large number may cause a server memory error. Optional.

Example:

// get second page of records with each page having 100. 
let searchParameters = {
  offset: 100,
  limit: 100
}

Specifying and limiting returned fields

fields_to_return (array) the fields to return. Optional.

Example:

let searchParameters = {
  fields_to_return = [ 'group_status', 'group_type', 'member_count', 'leaders', 'location_grid', 'last_modified', 'requires_updated' ]
}

Bringing it all together

After building the filter parameters, we need to transform the searchParameters object in the query parameters string. The query string needs to be the same format that jQuery.param() outputs. See here for a plain js alternative

let searchParameters = {
  overall_status: ["active", "-closed"] // -closed filters out the closed records
  seeker_path: ["none"]
  sort: "post_date"
  sources: ["instagram"]
}

let queryParametersString = jQuery.param(searchParameters)
// this gives a string that looks like this:
// seeker_path%5B%5D=none&overall_status%5B%5D=active&overall_status%5B%5D=-closed&sources%5B%5D=instagram&sort=post_date

//query away with:
let queryString = `https://example.com/wp-json/dt-posts/v2/contacts/?${queryParametersString}`;

Returns

//for contacts
{
  contacts: [
   { ... contact1 ... },
   { ... contact2 ... }
  ], 
  total: 339 // the total number of contacts available to page (see offset)
}
//for groups
{
  groups: [
   { ... group1 ... },
   { ... group2 ... }
  ], 
  total: 34 // the total number of groups available to page (see offset)
}