How to filter Phabricator tasks by custom fields programatically

1. Find out the ID of your field

The clue is that the fields have a seemingly-random name in the database. If you name your field “peopletoblame”, you can't easily filter by people_to_blame.

A dirty way to find out the id of the field is to add

var_dump($map);

in the first line of the buildQueryFromParameters function in phabricator/src/applications/maniphest/query/ManiphestTaskSearchEngine.php.

Then, perform a search on tasks and enter a value to your custom field in Maniphest search. New output will show up on the top of the screen:

Unexpected output:

array(30) {
  ["assignedPHIDs"]=>
  array(0) {
  }
  ["authorPHIDs"]=>
  array(0) {
  }
  ["statuses"]=>
  array(1) {
    [0]=>
    string(4) "open"
  }
  ["priorities"]=>
  array(0) {
  }
  ["subtypes"]=>
  array(0) {
  }
  ["columnPHIDs"]=>
  array(0) {
  }
  ["hasParents"]=>
  NULL
  ["hasSubtasks"]=>
  NULL
  ["parentIDs"]=>
  array(0) {
  }
  ["subtaskIDs"]=>
  array(0) {
  }
  ["group"]=>
  string(8) "priority"
  ["createdStart"]=>
  NULL
  ["createdEnd"]=>
  NULL
  ["modifiedStart"]=>
  NULL
  ["modifiedEnd"]=>
  NULL
  ["closedStart"]=>
  NULL
  ["closedEnd"]=>
  NULL
  ["closerPHIDs"]=>
  array(0) {
  }
  ["limit"]=>
  string(0) ""
  ["query"]=>
  string(0) ""
  ["ids"]=>
  array(0) {
  }
  ["phids"]=>
  array(0) {
  }
  ["subscriberPHIDs"]=>
  array(0) {
  }
  ["projectPHIDs"]=>
  array(0) {
  }
  ["spacePHIDs"]=>
  array(0) {
  }
  ["custom:wed0ni_vpOng"]=>
  array(0) {
  }
  ["custom:cbKyJ_0NwbeQ"]=>
  array(0) {
  }
  ["custom:Nyt0lw_r_2JS"]=>
  array(0) {
  }
  ["custom:Cgs.Ovu49H71"]=>
  array(1) {
    [0]=>
    string(30) "PHID-USER-ukyz5wmn4566anlt4stm"
  }
  ["order"]=>
  string(8) "priority"
}

Find the custom field id there.

Then, you can add the field to another query by adding:

->setParameter('custom:Cgs.Ovu49H71', array($viewer_phid))

in, for example, function buildSavedQueryFromBuiltin inside src/applications/maniphest/query/ManiphestTaskSearchEngine.php